Saturday, 15 July 2017

get location from ip in php

Get location from ip address.

<?php  
 $ip = get_IP_address();  
 $details = json_decode(file_get_contents("http://ipinfo.io/$ip"));  
 print_r($details);  
 echo "City: ". $details->city."<br/>";  
 function get_IP_address()  
 {  
   foreach (array('HTTP_CLIENT_IP',  
          'HTTP_X_FORWARDED_FOR',  
          'HTTP_X_FORWARDED',  
          'HTTP_X_CLUSTER_CLIENT_IP',  
          'HTTP_FORWARDED_FOR',  
          'HTTP_FORWARDED',  
          'REMOTE_ADDR') as $key){  
     if (array_key_exists($key, $_SERVER) === true){  
       foreach (explode(',', $_SERVER[$key]) as $IPaddress){  
         $IPaddress = trim($IPaddress); // Just to be safe  
         if (filter_var($IPaddress,  
             FILTER_VALIDATE_IP,  
             FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)  
           !== false) {  
           return $IPaddress;  
         }  
       }  
     }  
   }  
 }  
 ?>  


Download from this link.  click here

No comments:

Post a Comment

How to create an visitor counter in PHP | hit counter

Total page view in the php. This count hit in the page. <?php session_start(); if(isset($_SESSION['page_view'])){...