Is it possible to get the location details from the users IP Address in PHP.
Any suggestions ??
Thanks
$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
)
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
You can take a look to the maxmind database, and the GeoIP PECL extension.
In my case :
pecl install geoip
"/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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With