3

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 ?

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
user3405726
  • 51
  • 1
  • 1
  • 3
  • http://stackoverflow.com/questions/1576423/whats-the-simplest-way-to-get-a-users-latitude-and-longitude-from-an-ip-addres – Ravi Patel Mar 11 '14 at 11:09
  • The question you linked to isn't the same as this. This question is asking how to geolocate by IP server side, not client side. – David Barker Apr 28 '14 at 21:34

4 Answers4

6

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
Community
  • 1
  • 1
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
4

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!!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
عثمان غني
  • 2,786
  • 4
  • 52
  • 79
2

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.

pulsar
  • 986
  • 1
  • 9
  • 22
2

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!"); });
 }
SajithNair
  • 3,867
  • 1
  • 17
  • 23