Search engine optimisation from Blogstorm

Geotargeting with PHP : A complete guide

by Patrick Altoft on September 5, 2007

Geotargetting specific adverts, affiliate offers or content pieces is one of the most efficient improvements you can make to your website.

Spend an hour installing and configuring your geotargeting script and your revenue can increase dramtically.

Geotargetting is simply the art of showing different content to your visitors depending on which country they are from. For example if I have an affiliate offer that is only available to customers from the UK I know that it will be useless to US visitors. Using my script I will direct US visitors towards a similar product on Amazon or eBay.

Advertising networks such as DoubleClick and Adsense allow advertisers to target specific countries resulting in larger CPM payments and greater ROI for the advertisers so it clearly makes sense for you to do the same.

Other useful applications would include allowing advertisers on your site to target traffic from a certain location or to stop Yahoo Publisher Network ads showing for international users.

The first step towards installing your geotargetting script is to visit Maxmind and purchase a downloadable GeoIP database for $50. This database allows you to match up your visitors IP address to their country.

Once you have the database you will need to upload it to your site, I suggest keeping it outside your root directory or renaming it in case other people try to use it. The database file should be called geoip.inc.

Next you will need to add the script below to the top of each page on your website. If you have a main database connection file included already you could just add it to this.


if (isset($_COOKIE["geoip"])) {
$country = $_COOKIE['geoip'];
}
else
{
include(”/home/your_folder/geoip.inc”);
$gi = geoip_open(”GeoIP.dat”,GEOIP_STANDARD);
$country = geoip_country_code_by_addr($gi, $REMOTE_ADDR);
geoip_close($gi);

setcookie(”geoip”, $country, time()+3600, “/”, “.yoursite.com”, 0); //1 hour cookie

}

Using this information

Now we have stored the country of the visitor in a $country variable that can be used on each page of your site. To make use of this simply add the following code to your pages:


if($country=="GB"){
//UK offer
}
else
{
// worldwide offer
}

Combine this script with the outbound affiliate link redirection script and you have a perfect money making machine.

If you have any questions please post below.

You can get our blog posts delivered for free by email every day - simply add your email address to the box below or alternatively grab the RSS feed.

Read some similar posts

Published in: Affiliate Marketing, Coding delicious | digg | reddit | StumbleUpon | Google Bookmarks | Sphinn

{ 2 trackbacks }

Links for 17-02-2008 | Velcro City Tourist Board
02.17.08 at 3:18 pm
Bir
09.22.08 at 6:00 pm

{ 7 comments… read them below or add one }

1 Vittorio 06/09/2007 at 1:39 am

Hey Patrick,

I just wanted to point out that MaxMind offers a free IP2Country database here:
http://www.maxmind.com/app/geolitecountry

I also found another free database here:
http://software77.net/cgi-bin/ip-country/geo-ip.pl

Hope this helps.

2 Peter 09/09/2007 at 7:32 am

I put this code on my website with about 3000 uniques a day but not with cookies implemented. (I got this code from another website but I forgot to implement the cookies part). About a week later I had my site suspended by Lunarpages for using too many resources. (I’m on their cheapest $6-7 a month plan)

I wonder how much improvement the cookies would have regarding resources. Perhaps I could get around 6000 uniques a day before I start going over allocated resources.

Also I had an idea.. wouldn’t it be better to remove entries for countries you’re not going to use?

I only test for two countries, so having another 100+ countries and hundreds of thousands of extra entries would just be a huge burdern, is it possible?

cheers,
Peter

3 DerekBeau 10/09/2007 at 5:46 am

Nice article. I also wrote a Geotargeting article that discusses the MaxMind database and a free web API. Here’s the link if you want to check it out:

4 Chris Jackson 25/02/2008 at 11:14 pm

In response to Peter, RE: host resources, you should almost always set up a cookie alternative - as each page load requires your server to go through 1MB of data to figure out where the user is coming from. If each user looks at say 5 pages - this will put a huge burden on the server - which is exactly why your site was closed down. The cookie serves as a good work around to save you having to look the info up EVERY time.

5 Suzy Turnbull 20/03/2008 at 2:46 am

Geo targeting is useful in the instances you have cited, however, with language this is a problem if your native language is different to the location/language your IP address is being identified from. Better to allow customer to choose their language of preference on your site.

6 Patrick Altoft 20/03/2008 at 2:57 am

I agree, this is covered more over here.

7 Roman 02/08/2008 at 11:06 am

Hi, nice idea, but IE doesn’t access the website then. Don’t you know why?

Leave a Comment (get an avatar from Gravatar first)

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>