Locating IP-Adresses using Maxminds GeoIP

01.03.2014 | PhilVanB | Programmierung | PHP

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.

Linux based installation of Maxminds GeoIP library

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

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

In the next Step php-geoip will be installed:

sudo apt-get install php5-geoip

Usage of Maxminds GeoIP with PHP

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('');

if ($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: https://www.php.net/manual/en/geoip.setup.php