Locating IP-Adresses using Maxminds GeoIP

Saturday March 1st, 2014 | by PhilVanB | de_DE Dieser Artikel auf deutsch


If, concerning Webdevelopment, locating IP-Adresses is of any interest for you, for instance in order to detect the point of origin of your websites’ visitors, this article is just right for you. Preassumption is that you are using a Linux-System.

To begin with, you first of all need Maxmind’s GeoIP-database. Download the free GeoLiteCity.dat. You can get it from here: http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

Unzip this file. Now, create a new directory in /usr/share/ named GeoIP:

 

sudo mkdir -v /usr/share/GeoIP

 

Then move the downloaded GeoLiteCity.dat into the new directory:

 

sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

 

That way the file is also renamed to GeoIPCity.dat. It’s important to rename the file that way. Otherwise you’ll get an error-message later on.
Make sure to change the director permission of GeoIP to 777:

 

sudo chmod 777 /usr/share/GeoIP -R

 

That’s it so far with all the Installations. Now the coding. Create a new php-script on your server, for example a file geoIPtest.php under /var/www/

That would be the code for the file:

 

$record = geoip_record_by_name('64.233.160.0');
 
 if ($record) {
 
      print_r($record);
}

 

The IP-Adresses are passed to the function geoip_record_by_name(). You can also pass an URL such as www.google.com.

The script geoIPtest.php should create the following result:

 

Array (
 
    [continent_code] => NA
    [country_code] => US
    [country_code3] => USA
    [country_name] => United States
    [region] => CA
    [city] => Mountain View
    [postal_code] => 94043
    [latitude] => 37.419200897217
    [longitude] => -122.05740356445
    [dma_code] => 807
    [area_code] => 650
)

 

That way you’re able to gather information such as City, Postal-Code or Country simply by passing one IP-Adress.

If you’re looking for more information about this topic, take a look at this link: http://de2.php.net/manual/de/geoip.setup.php

 

 



>