Installing And Using The New Maxmind City Location Databases On Your Own Server

GeoIP Databases and Device Detection

GeoIP databases are what we use to correspond a user’s IP to a country or city location, as well as ISP. There are several providers of such databases and one of the most well known as Maxmind.

Maxmind provide some free country and city databases that are less accurate than their paid versions. However their organisational (ISP) database is not free.

Most tracking systems will have databases integrated already. You can also use these databases on your server to do things like call out a user’s city and a landing page.

A good tutorial on installing and using these databases is after this post

As I said there are multiple vendors for these databases, which is why you can get some disagreement between your tracking system and an affiliate network for example.

The IPs used by various ISPs and thus their users are also in a state of change, hence the need for reasonably regular database updates if you want to remain accurate.

In regards to accuracy, most GeoIP databases are not that great at detecting user city outside of large metropolitan areas – especially in countries outside the US and UK. One of the main reasons for this is that your gateway, the computer that your router/modem connects to when you use the Internet, is often located in a nearby city if you’re in a small town.

Calling out use a city or state for example on a landing page can help increase click through rates and conversion rates by making the lander appear personalised to that user. However, this can backfire if the result is completely wrong, thus it is something that should be split tested and the code used to do this should fall back to some default option if the city/state can’t be resolved.

Device/handset detection is similar and generally uses the user-agent header that browsers supply.

Much like GeoIP databases there are several suppliers for handset detection. One of the most commonly utilised is the WURFL databases, which are free.

Detecting and logging devices is crucial for optimising mobile campaigns because different devices can perform drastically differently.

Tablet and smartphone traffic often performs differently. Certain makes and models simply won’t convert for a specific campaign. Who knows or cares why, the data just says so.

You can also personalise landers by calling out device make and model though this can be difficult when models names aren’t very friendly – and the format of available data depends on the detection databases used.

If your tracking system has device detection (hint: it will) you can usually also pass device make and model through in the URL dynamically, i.e. the tracker detects this information and then inserts it in the URL so that your landing page can use it.

That’s all you really need to know for now – you don’t need to start calling out cities, countries, device makes and models in your landing pages immediately but you should at least track the data for every campaign. What you do with that data is up to you!

Installing And Using The New Maxmind City Location Databases On Your Own Server


So, as you may have heard, MaxMind (the best geolocation service out there) is in the process of changing how they do business. That might mean that, if you’re using their Javascript geolocation, things suddenly start costing money or stopping working.

For that and a variety of other reasons, it’s probably better to run MaxMind scripts from your own server – but it’s also a bit harder to do.

STM to the rescue! Here’s a guide to installing the latest MaxMind city and country location database and using it on your own server.

What you’ll need

  • Command-line (SSH) access to your server. If you don’t have that, you can ask your hosts to install the MaxMind GeoLite 2 databases for you – if necessary, feel free to copy/paste the “Installing The Database” section to them.
  • PHP 5.3 or greater. If you’re on a reasonably modern server, you’ll have that – if in doubt, ask your hosting provider.
  • Basic familiarity with the Linux command line. You don’t need to be a sysadmin god, but you’ll need to be able to change directories! If you’re not confident about that, ask your hosting provider to do the installation part for you – see “Installing The Database”.

What we’ll achieve

By the end of this tutorial, you’ll have the latest MaxMind databases installed on your own server. That means that:

  • Your landing pages will no longer depend on MaxMind’s service, meaning that if they decide to shut it down or change it, your profits won’t be affected.
  • You’ll be using the latest database for IP address lookups, which means that more visitors to your LP will have their location correctly identified.
  • You’ll have more options for locations in your LPs – not just city, but also region and even latitude/longditude.

Step 1: Installing The Database

The installation process for the new MaxMind PHP database looks complicated, but it’s simpler than it looks. Here’s the simple way to do it:

1) Log into your server and change directory to the root of your web directories. (That’ll be /usr/local/apache2/htdocs by default on Apache servers, and /usr/local/nginx/html or /usr/share/nginx/www for Nginx.)
3) Create a directory called “geolocation” under that directory. Change directory to it.
2) Download the MaxMind database to the directory you just created. (You can actually put it in any directory you like, but I’m recommending this one for simplicity.)

You can either download it from this page to your local computer, then unzip it and upload it via SFTP/FTP, or you can just go to your SSH client and type

tar zxvf Geolite2-City.mmdb.gz

Note: on some versions of Linux, for an unknown reason, wget won’t work to download this file. In that case, just download it onto your home computer, unzip it and upload

3) Change back to your root directory.
4) Now, we need to install Composer, which manages the code libraries for the new GeoIP platform. Run

curl -sS | php

If it throws any error messages, do what they suggest – they’ll usually come with very clear instructions to add a line to a PHP configuration file or similar.

5) Make a file called “composer.json” in the root of your web directory, and paste the following lines into it:

    "require": {
        "geoip2/geoip2": "0.4.*"

Now, run

php composer.phar install

That’ll automagically install everything you need for GeoIP!

Step 2: Code For Your Landers

OK, that’s the hard bit over with.

To make this detection code work on your landers, just add the following PHP at the start of your landing page:


require $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php';

$ipaddress = $_SERVER["REMOTE_ADDR"];

use \GeoIp2\Database\Reader;
$reader = new Reader($_SERVER['DOCUMENT_ROOT'].'/geolocation/GeoLite2-City.mmdb');
$record = $reader->omni($ipaddress);


That code looks up the visitor’s IP address, and compares it against the database to get a whole bunch of useful information.

Now, we can access that information whenever we want.

Displaying user’s city

The simplest thing you’ll probably want to do is insert the name of the user’s city. You can do that adding:


wherever you want the city name to be displayed in your landing page.

It’s worth knowing that MaxMind sometimes doesn’t find a city name, and so displays a blank.

In that case, you can display something else – like “Your City”, say – by adding the following code right below the code you added at the start of the landing page:

<?if ($record->city->name == '') $record->city->name = 'Your City';?>

Other Things You Can Do On Your Landers With This Code

  • You can display a user’s state, rather than city, by using <?=$record->mostSpecificSubdivision->name;?>
  • Or you can display their country using <?=$record->country->name;?>
  • You can also do a SWITCH statement or IF statement on their country to, for example, redirect Facebook approvals people to the right page. It’s easiest to do this with the country’s ISO code (“US”, “GB”, etc), which you can get with $record->country->isoCode .
  • You can get the longditude and latitude of the user with $record->location->longditude or $record->location->latitude. You can use this to get a list of nearby cities – I’ll write a tutorial on how to do that soon!
  • You can find out how accurate this lookup is likely to be with $record->city->confidence – and if it’s below a given value, say 50, you can display a different piece of text or even an entirely different lander!
  • You can access city names in other languages using the $record->city->names array. I’ll be honest and say I haven’t 100% figured out how to do this, but it’s possible!
  • You can detect if the IP belongs to an anonymous proxy with $record->traits->isAnonymousProxy . Proxied IPs might well be spy accounts, so you could theoretically then redirect them or show them an entirely fake lander!

There’s loads of other stuff, too – you can find out ISP name, type of connection, and more. I’m pretty excited about all the new options in this library. It’s a bit tricky to wade through, but the API documentation has all the details.

And that’s it! Let me know if you’ve found this tutorial useful, if you were confused by any points or if they didn’t work, or if there’s more information or a related tutorial you’d like to see!

Download WordPress Themes
Download WordPress Themes
Download WordPress Themes
Download Best WordPress Themes Free Download
udemy course download free
download karbonn firmware
Free Download WordPress Themes
udemy free download

      Dani Master