How get latitude and longitude by client ip?

i use this function to get client IP it work

function get_client_ip() {
    $ipaddress = '';
    if ($_SERVER['HTTP_CLIENT_IP'])
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if($_SERVER['HTTP_X_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if($_SERVER['HTTP_X_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if($_SERVER['HTTP_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if($_SERVER['HTTP_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if($_SERVER['REMOTE_ADDR'])
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';

    return $ipaddress; 
}

$ip = get_client_ip();

but how i can get latitude and longitude by sever side and not client side ?

Related posts

Leave a Reply

4 comments

  1. PHP does not have the in-built support for that. You could make use of third party libraries like ip2location to grab the longitude and latitude from the ip address.

    Sidenote : Determining the latitude – longitude through an IP Address may not fetch you accurate information.

    Uses geoplugin.net. See if this suits you.

    <?php
    $new_arr[]= unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
    echo "Latitude:".$new_arr[0]['geoplugin_latitude']." and Longitude:".$new_arr[0]['geoplugin_longitude'];
    

    OUTPUT :

    Latitude:180.240601 and Longitude:12.9819
    
  2. Best option from php.net which is also open source: Geo IP

    You can install it using following steps. Which are given on the link I have provided:

    Run these commands in terminal.

    wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
    gunzip GeoLiteCity.dat.gz
    sudo mkdir -v /usr/share/GeoIP
    sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat
    
    sudo apt-get install php5-geoip
    

    Hope it helps!!

  3. You should consider getting the exact location of the user from client side using Javascript API navigator.geolocation. The advantage of using this is, it will be accurate.

    The issue with IP to Geo is that, there is no guarantee that the info will be accurate. It depends on how updated the data is.

    On the other hand, the javascript API directly takes the geo based on GPS or Network address (whichever is accurate). But the user will have to give permission to access to his GPS.

    You can use the Javascript (if user gives permission) or fall back to ip to geo conversion

    How to Use

     if (navigator.geolocation && navigator.geolocation.getCurrentPosition) {
          navigator.geolocation.getCurrentPosition(function(position) {
              // save location by sending to server
          }, function() { alert("Couldn't get your position!"); });
     }
    
  4. For a quick, copy-and-paste solution, I have written code for accomplishing this task.

    I use this function:

        function getUserIP()
        {
            $client  = @$_SERVER['HTTP_CLIENT_IP'];
            $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
            $remote  = $_SERVER['REMOTE_ADDR'];
    
            if(filter_var($client, FILTER_VALIDATE_IP))
            {
                $ip = $client;
            }
            elseif(filter_var($forward, FILTER_VALIDATE_IP))
            {
                $ip = $forward;
            }
            else
            {
                $ip = $remote;
            }
    
            return $ip;
        }
    
        $ipAddr = getUserIP();
    
        $geoIP  = json_decode(file_get_contents("http://freegeoip.net/json/$ipAddr"), true);
    
        echo 'lat: ' . $geoIP['latitude'] . '<br />';
        echo 'long: ' . $geoIP['longitude'];
    

    Outputs:

    34.0731
    118.3994
    

    It uses the (free) freegeoip.net API which I have found to be extremely fast with a comprehensive database. Being free, it has a limit of 10000 requests per hour (just under 3 per second), which should suffice for most projects’ needs.