In this tutorial I will show you how to implement a free php geo-targeting system, as well as some paid alternatives for larger systems should you need it.

Geo-targeting is a useful technique to use to enable you to provide an individual service depending on a users location, track where your visitors are from for statistical use, direct to download mirrors and so on.

To use this service you will need to provide the database with an IP address and it will compare this to stored ranges to provide you with locations of varied accuracy depending on provider. The problem with this is that these databases are rarely free and would take a long time to compile yourself. To combat this you will need to find a database for you to query, whether this be a free provider or a paid one.

Free providers tend to provide a more stripped down database of locations with less accuracy than that of a paid service, which is to be expected.

Getting The Users IP

Using PHP you can access a users IP address to be used in the geo-targetting. It is a very easy variable call too access this:

<?php echo $_SERVER['REMOTE_ADDR']; ?>

The problem with this is that it is not always very accurate and could be hidden by a proxy address. In an attempt to combat this you can use this simple function that will aim to get a more accurate reading:

<?php
function get_ip()
{
    if( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
    {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
?>

Connecting To The API

The API is very simple to use though it returns the text is a plain format and not XML as you would expect. In the next section I will be showing you how to parse this information easily. For the initial call you will only need to send over an IP address and it will return with an array of information.

http://api.hostip.info/get_html.php?ip=89.243.168.26&position=true

Will return the following result. As you can see it does not appear to have a location for my IP address which is a bit worrying, though I cannot vouch for how good it is in general.

Country: (Unknown Country?) (XX)
City: (Unknown City?)

Latitude:
Longitude:
IP: 89.243.168.26

Handling The API Output

Then you can start handling the returned data with the following PHP snippet. The snipper basically matches content after a semi-colon and before the line break, it is then just a case of assigning variables depending on what order they come in.

<?php
// Get the IP - see previous stated function
$client_ip	= get_ip();

// Ping the site
$get_loc	= file_get_contents( 'http://api.hostip.info/get_html.php?ip=' . $client_ip . '&amp;position=true' );

// Parse for location
preg_match_all( "|:([A-Za-z0-9-_()?!&amp; ]+)\n|", $get_loc, $matches, PREG_SET_ORDER );

// Assign variables
$city = trim( $matches[1][1] );
$lat  = trim( $matches[1][2] );
?>

If you are looking for a more in depth geolocation database then there are quite a wide selection of paid options available, though one I know of is MaxMind GeoIP. If you know of any others please leave a comment.

Tagged as: , , , ,