BEST-WEB-TOOLS Blog

My own blog posts about development, tech, finance and other (interesting) stuff.

❮❮ back

2022-12-06, Dev, MaxMind, GeoIP, GeoIP2, Location, IPv4

Working with MaxMind IP Database and PHP

Today still many web applications working with ip location based infos. GeoIP can help you find out the country or even city of your current user, setting a local timezone on the server-side or getting data about the hosting provider (ASN organization) responsible for the IP address.

Data from MaxMind GeoIP Database

The company MaxMind provides a free to use IP database for IPv4 and IPv6 addresses. It is a bit hidden on their website. You can find infos and the download if you are looking for GeoLite2 Free database. After creating a free account you can download different versions of the database.

  • GeoLite2 Country - Country name, iso code and continent
  • GeoLite2 City - All country data plus city infos like city names in different locales geo position of the city (in lat, lng) and local timezone (see examples below)
  • GeoLite2 ASN - ASN (Autonomous System Number) and organization that managing the IP address

The MaxMind license is a variant of Creative Commons ShareAlike 4.0 and can be also found on the MaxMind website.

MaxMind GeoLite2 Database

After you have downloaded the database you will have a file with the extension .mmdb (MaxMind DataBase). This file format is a binary and proprietary format by MaxMind. You can also find CSV variants of the data or even APIs (web services). But the most performant and smallest in disk size is the MMDB version of the data. The binary city database is currently 70mb and ASN database is 8mb in size.

You can read more about the binary format on in the MaxMind documentation but it is not necessary at all to know the format if you want to work with it. MaxMind provides SDKs for all major programming languages like Java, .NET (C#), Ruby, Python, Node.js and even PHP.

You can find all the SDKs on GitHub and the PHP SDK is installable via Composer package manager:

composer require geoip2/geoip2:~2.0

Working with GeoLite2 and PHP

After installing the package in your project you can use the database reader class to search and fetch data from your local GeoIP database.

use GeoIp2\Database\Reader;
$reader = new Reader('/local/path/to/GeoIP2-City.mmdb');
$record = $cityDbReader->city('8.8.8.8');
echo $record->country->name;

Updating the MaxMind Database

After you created an account on the MaxMind website you can find an overview of available downloads in your dashboard. Every version of the database has a permalink with your license-key (API key) in the url that can be used to automatically download the database as tar.gz. The databases getting updates every day so you should write a script that fetch the new version daily.

MaxMind also provides an automatic updater script that could be installed via apt on linux (for example ubuntu). On the website are more infos about setup and configuration.

sudo add-apt-repository ppa:maxmind/ppa
sudo apt update
sudo apt install geoipupdate

PHP Examples of MaxMind IP Database

The most common fields are location, city and country. Not every IPv4 address has all the data available.


use GeoIp2\Database\Reader;
$reader = new Reader('/local/path/to/GeoIP2-City.mmdb');

$record = $cityDbReader->city($_SERVER['REMOTE_ADDR']);
echo $record->country->name;

echo get_class($record)."\n"; # GeoIp2\Model\City

echo $record->location->latitude."\n"; # 38.894
echo $record->location->longitude."\n"; # -77.0365
echo $record->city->name."\n"; # Washington
echo $record->country->isoCode."\n"; # US


# get local timezone and time
if($record->location->timeZone) {
    
    echo $record->location->timeZone."\n"; # America/New_York

    $localTime = new \DateTime();
    $localTime->setTimezone(new \DateTimeZone($record->location->timeZone));
    
    echo $localTime->format('H:i');
}


❮❮ back