Geotargeting with PHP : A complete guide

  • 0
  • September 5, 2007
Patrick Altoft

Patrick Altoft

Director of Strategy

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

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'];
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$country = geoip_country_code_by_addr($gi, $REMOTE_ADDR);

setcookie(“geoip”, $country, time()+3600, “/”, “”, 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:

//UK offer
// 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.

Free of charge. Unsubscribe anytime.