How to determine someone's city from their IP address
-
Let's assume that you want to determine a city by IP in your PHP script.
In this example we will use the GeoIp library.
-
Download GeoIP PHP-module.
-
Download free cities and countriesdatabases
-
Include geoipcity.inc and geoipregionvars.php files from the GeoIP module to your script.
for example:
include("geoipcity.inc");
include("geoipregionvars.php");
-
Include the script city DB to your script.
for example:
$gi = geoip_open("/home/data_old/GeoIP-132_20070101/GeoIPCity.dat" , GEOIP_MEMORY_CACHE);
-
Add the string
$record = geoip_record_by_addr($gi,"your_IP");
into your script.
Object $record will now contain all the needed information.
For IP 212.58.251.202 $record will be
geoiprecord Object
(
[country_code] => GB
[country_code3] => GBR
[country_name] => United Kingdom
[region] => B8
[city] => Tadworth
[postal_code] =>
[latitude] => 51.2833
[longitude] => -0.23339999999999
[area_code] =>
[dma_code] =>
)
-
Finally close the GeoIP DB:
geoip_close($gi);