views:

278

answers:

4

Hi All,

Is it possible to get the location details from the users IP Address in PHP.

Any suggestions ??

Thanks

+2  A: 

You need to use some kine of Geo IP Service

One free service i found on google: geoplugin. They php snipplets to use their service: geoplugin/php

Am
+1  A: 

Check out the PEAR GeoIP library.

Daniel Schilling
you link returns 404
Am
Sorry about that. The underscore got encoded. Fixed.
Daniel Schilling
+3  A: 

You can take a look to the maxmind database, and the GeoIP PECL extension.

In my case :

  • I've installed the extension with "pecl install geoip"
  • And I've downloaded the geolitecity database and copied it to /usr/share/GeoIP/GeoIPCity.dat so it's found by the PECL extension.

Note there should also be some PEAR package (PEAR::Net_GeoIP) to help you, if you cannot install any PECL extension.


Once you have installed both of those, you can use this kind of code :

$ip = '82.229.x.y';     // replace with your IP address
var_dump(geoip_record_by_name($ip));

And you'll get this kind of output :

array
  'continent_code' => string 'EU' (length=2)
  'country_code' => string 'FR' (length=2)
  'country_code3' => string 'FRA' (length=3)
  'country_name' => string 'France' (length=6)
  'region' => string 'B9' (length=2)
  'city' => string 'Lyon' (length=4)
  'postal_code' => string '' (length=0)
  'latitude' => float 45.75
  'longitude' => float 4.84999990463
  'dma_code' => int 0
  'area_code' => int 0

Which, in my case, is true : I am indeed in the city of Lyon, FR.

Pascal MARTIN
+2  A: 
$ip = '98.229.152.237';
$xml = simplexml_load_file("http://ipinfodb.com/ip_query.php?ip=$ip");
print_r($xml);

Output:

SimpleXMLElement Object
(
    [Ip] => 98.229.152.237
    [Status] => OK
    [CountryCode] => US
    [CountryName] => United States
    [RegionCode] => 33
    [RegionName] => New Hampshire
    [City] => Manchester
    [ZipPostalCode] => 03103
    [Latitude] => 42.9403
    [Longitude] => -71.4435
    [Timezone] => -5
    [Gmtoffset] => -5
    [Dstoffset] => -4
)
David Barnes
thanks this is what i was looking for ....
Biranchi