How to Replace PHP GeoIP with MaxMindDB

Posted on by David Singer | Updated:
Reading Time: 3 minutes

Depending on the site or application, looking up geographic information related to an IP address can be a pretty common action. When doing IP geolocation in PHP usually the PHP GeoIP extension would be used to facilitate the retrieval of this information. Unfortunately, this particular plugin is no longer actively supported and has not been updated in a number of years.

With the go-to PHP extension of IP geolocation effectively being deprecated, new projects should begin to use the replacement options that are now provided by MaxMind. However, unlike the original GeoIP, which was shipped as a native PHP extension, the new solutions are provided as PHP-based library packages.

Requirements

  • Basic familiarity with PHP coding is necessary to utilize Composer packages.
  • Command line access via SSH may be necessary to follow this tutorial.
  • Composer, Curl, funzip must be available on the server.

Step #1: What are my options now?

As mentioned previously, the new options are no longer provided as native PHP extensions, but rather they come as Composer-based PHP packages. The new options provided by MaxMind are either: GeoIP2-php or DB-Reader-php.

Both of the options provide the ability of IP geolocation with subtle differences; in a sense the GeoIP2-php package is an addition built on top of the DB-Reader-php package, it offers all the same features as DB-Reader-php with the addition of API access.

Additionally, it is important to note that only the DB-Reader option is provided without any additional costs. With the new options MaxMind now charges a subscription fee in order to access their APIs.

Generally for most use-cases the necessary features will be provided by the DB-Reader-php package, as such this article will focus on this option.

Step #2: Get started with MaxMind DB-Reader!

As mentioned in the requirements, Composer will be required in order to follow these steps. If you do not have Composer ready to go, learn how to set up and use Composer on your server.

To acquire the necessary DB-Reader package you will want to start by changing directory so that you are in the root folder of your domain (for this example we will just assume this is `public_html`), then you will run the following command.

[public_html] $ composer require maxmind-db/reader:~1.0

Running this command will download the package files into the current folder, as described in our Composer series. This will create a vendor folder if this is the first time using Composer in this site/project.

Next, you will require the actual MaxMind database files which contain the geolocation data. To get these files you will execute the following commands.

[public_html] $ funzip <(curl -L http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz) > ./GeoLite2-Country.mmdb
[public_html] $ funzip <(curl -L http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz) > ./GeoLite2-City.mmdb

With the above commands executed you should now have the necessary components to do Geolocation using the DB-Reader plugin. All that’s left is to implement it in your code.

Step #3: Looking up your first IP

In order to ensure the geolocation features are working properly you may want to do a quick test. First we’ll confirm you have all the right pieces by running the following command.

$ ls -lah

You should see a file structure similar to this.

-rw-rw-r-- 1 someuser someuser 63 Aug 11 17:03 composer.json
-rw-rw-r-- 1 someuser someuser 2.4K Aug 11 17:03 composer.lock
-rw-rw-r-- 1 someuser someuser 73M Aug 11 17:04 GeoLite2-City.mmdb
-rw-rw-r-- 1 someuser someuser 19M Aug 11 17:04 GeoLite2-Country.mmdb
drwxrwxr-x 4 someuser someuser 4.0K Aug 11 17:03 vendor/

Now you are ready to do a quick test, you can do so by creating an index.php file with the following content.

<?php
require_once 'vendor/autoload.php';

use MaxMind\Db\Reader;
$ipAddress = '8.8.8.8';
$databaseFile = './GeoLite2-City.mmdb';

$reader = new Reader($databaseFile);

print_r($reader->get($ipAddress));

$reader->close();

This index file will simply do a quick test to ensure that the database file is being loaded, an IP can be checked, and the results are being provided. The test will be looking up the geographic information related to Google’s DNS server at 8.8.8.8.

MaxMind Results
Geo-location results for Google's 8.8.8.8 DNS server via MaxMind

Having followed the directions correctly you should see output similar to the image above when loading the test index page.

Avatar for David Singer

About the Author: David Singer

I am a g33k, Linux blogger, developer, student, and former Tech Writer for Liquidweb.com. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Latest Articles

Blocking IP or whitelisting IP addresses with UFW

Read Article

CentOS Linux 7 end of life migrations

Read Article

Use ChatGPT to diagnose and resolve server issues

Read Article

What is SDDC VMware?

Read Article

Best authentication practices for email senders

Read Article