Where are you from? IP Geolocation
Written by Harry Fairhead   
Wednesday, 15 February 2012
Article Index
Where are you from? IP Geolocation
Finding the IP address

Knowing where a website visitor is located is vital if you hope to have a chance to provide information that is geographically relevant. The simplest solution is to use IP geolocation.

In the case of I Programmer, for our book review section and articles such as this one where we indicate relevant book titles, we want to pinpoint a user's location so that the best Amazon locale can be used to retrieve book pricing details in the correct currency. 

One obvious solution would be to ask the user but, guess what, users really don't like to be asked things. Indeed if confronted by a pop-up window demanding that they divulge where they are, many visitors might simply go away without ever finding out if a site offers anything useful.

 

Banner

 

For some reason most users are paranoid about revealing any information, no matter how innocent and harmless even if it could be beneficial. So, we need an automatic way to determine with reasonable accuracy where the user is.

We also need a facility to override the choice - users for some reason are always pleased to correct personal data that you have wrong even if they won't supply it in the first place!

Using MaxMind's GeoLite

In principle, all we need to do the job is a big table mapping IP address to location.

Sadly there isn't a standard 100% certified IP to location table. As a result a number of companies have put in a lot of effort and created exactly such tables and of course sell them and/or their services.

One such company is MaxMind which offers a range of location services including the free GeoLite Country. This is a downloadable database complete with access software. There is an upgrade to a paid service which increases the accuracy from 99.5% to 99.8% and throws in additional features.

The free version is certainly enough to prove the worth of the idea. If you need to locate your users to an individual city then there is also GeoLite City and its corresponding paid-for full version.

The first thing to do is to download (about 700KB at the moment) the GeoLite Country Binary Format, GeoIPdata.gz, from the MaxMind website

http://www.maxmind.com/app/geolitecountry

This takes the form of a .gz compressed file and you need to decompress this, use 7-Zip if you don't already have a decompression utility.

The resulting file, GeoIP.dat, is currently around 1Mbyte and this has to be stored somewhere accessible on your web server. As I'm working with a Joomla PHP based website I stored it in:

/includes/GeoLite

To work with it from PHP you also need to download geoip.inc which contains the code needed to access the database. This also needs to be stored on the server and the most sensible location is in the same directory as the database, i.e. in my case, /includes/GeoLite.

Making it work

Time to start work on the actual program, which couldn't be easier.

Create a new PHP file called findRegion.php - again you might as well store it in the same directory. So now you should have a /includes/GeoLite directory containing:

GeoIP.dat
geoip.inc
findRegion.php

The API has facilities to specify the country in different ways, but the country code (e.g. "GB", "US", "FR", etc.) is one easy option.

If you would like to know what the country codes are then see:ISO 3166-1 alpha-2.

Instead of having to determine the country of the client every time they visit the site we are going to set a cookie so that any other script in PHP or Javascript can use the region information. The user is also given a menu option to change the region and reset the cookie to a new value. Likewise, if it already exists we don't want to change it.

We start off by checking that the cookie called Region exists. If it does, the job is nearly done and we simply set the $Region variable so that we can refresh the cookie at the end of the program by recreating it.

<?php
if(isset($_COOKIE['Region']))
{
$Region = $_COOKIE['Region'];
}
else

Only if the cookie isn't set do we do the actions in the else part of the if, so this makes the whole thing efficient.

To make use of the GeoLite code we have to load the include file:

{ 
$GeoPath=JPATH_BASE .DS.'includes'.
DS.'GeoLite'.DS;
include($GeoPath.'geoip.inc');

As I Programmer is a Joomla website, the standard variables JPATH_BASE and DS have been used to provide the start of the URL and the directory separator. If you're not using Joomla simply build the path in a way that works for you, e.g. as simple as:

$GeoPath='/includes/GeoLite/';

 

Banner

<ASIN:0596007035>

<ASIN:0321518667>

<ASIN:1449304729>

<ASIN:0321733452>

<ASIN:143022925X>



Last Updated ( Friday, 17 February 2012 )