Geotargetting Adverts by IP Address of Visitor

For one of my sites I use some geotargeting code (plus a database of IP addresses) to show content to people in different countries – mostly for targeting adverts to the USA or UK in my case, but specific content can be shown to any country.

Now, unfortunately, I did not document exactly how I got mine working when I set it all up, but I do recall that I used some information from here: http://maxmind.com

– but I think that may have just been the free/open source country IP database. The other parts of the solution were taken from another site, specifically with the purpose of only showing Yahoo adverts to US readers. After doing a quick search, there are now several sites that provide these tutorials, and I cannot remember which, if any, I used. So here is my own little lesson on Geotrageting content.

For this solution to work you need to be able to parse php on your pages.

First, the following code needs to be placed in the body of on each page that you wish to place geotargeted content on:

<?php
/*
include(“geoip.inc”); // include the geoip functions
$geofile = geoip_open(“GeoIP.dat”,GEOIP_STANDARD);
echo geoip_country_code_by_addr($geofile, $_SERVER[‘REMOTE_ADDR’]);
echo ” — “;
echo geoip_country_name_by_addr($geofile, $_SERVER[‘REMOTE_ADDR’]);
geoip_close($geofile);
*/
include(“geoip.inc”); // include the geoip functions
$geofile = geoip_open(“GeoIP.dat”,GEOIP_STANDARD); // open the geoip data file
$cc = geoip_country_code_by_addr($geofile, $_SERVER[‘REMOTE_ADDR’]);
geoip_close($geofile); // close the data file
?>

The code to geotarget is like this – with some random ad rotator code added:

<?php
if($cc == “US”) {
// It’s a US visitor.
$ads = array(); // Start an array

$ads[] = ‘
Hello America
‘;
//Content 1

$ads[] = ‘
Howdy
‘;
//Content 2

//We have no added another. Now we will display the ad’s.
shuffle($ads);
// We will use the PHP function shuffle to randomise our advertisements.
echo $ads[0];
// We echo the first ad in the array. The array has been shuffled so the first ad will change.

} // end if US

elseif ($cc == “GB”) {
// It’s a GB visitor.
//Be sure to precede all “quotes” in the ad code with backslashes as shown in the example.
$ads = array(); // Start an array
$ads[] = ‘
Good morning, would you like some tea?
‘;
//Brits

$ads[] = ‘
How’s the weather?
‘;

//We have no added another. Now we will display the ad’s.
shuffle($ads);
// We will use the PHP function shuffle to randomise our advertisements.
echo $ads[0];
// We echo the first ad in the array. The array has been shuffled so the first ad will change.
} // end elseif GB

else {
//Be sure to precede all “quotes” in the ad code with backslashes as shown in the example.
echo ”
Rest of the world message
“;
} // end else
?>

In addition to this, you then also need two files in the directory that has pages that you are targetting (I wish I knew how to link direct to the files, as I duplicate these files all across my site!)

They are:
geoip.inc
GeoIP.dat

geoip.inc is here: link to file

and GeoIP.dat is the database that you can download from somewhere, like Maxmind.

I got all this for free from somewhere. I really wish that I bookmarked it at the time! Been using this for a year or so now. Nothing was paid for, all open source or provide free to use and share.

Hope it proves useful to someone!