How to geotarget ad delivery.

I decided to write a step by step guide on how to geotarget ads to the lower paying countries as I've had a few people ask me how to do it recently.

All you will need is GEOIP Lite from maxminds.com (You can use the commercial version if you like).

The steps:

Step 1.

Download geoip.dat from maxminds.com HERE

Step 2.

Download the free Lite geo country file called geoip.inc HERE.
There is also a commercial version that costs $50 for the download and $12 a month to keep it updated. The paid version recognises 99% of ips instead of 95%.

Step 3.

Extract the files and upload those 2 files to your domains root directory (or anywhere else you want I guess).

Step 4.

Edit your page and add these lines to the top of the file.

<?php
if (isset($_COOKIE["geo"])){
$country = $_COOKIE['geo'];
} else {
include("/path/to/geoip.inc");
$gi = geoip_open("/path/to/GeoIP.dat",GEOIP_STANDARD);
$ip = $_SERVER["REMOTE_ADDR"];
$country = geoip_country_code_by_addr($gi, $ip);
geoip_close($gi);
setcookie("geo", $country, time()+15552000); //6 month cookie }
}
?>

Step 5. (inserting ad code into your pages)

Add where you would like the ad to appear.

<?php
if ($country == "CN") {
echo 'insert ad code';
}
?>

To target additional countries you can modify the code like this

if( $country == "CN" || $country == "IR" || $country == "PL" || $country == "PH")

The country codes can be found here --> Country Codes for Geo Targeting

NOTE:
The above guide is for placing a single ad code into your pages but you can also use the same technique to geotarget 2 different ad codes by having two ad codes that are called depending on the location of the visitor.
for eg.

<?php
if (country == "US")
{
$ad = 'ad code 1';
}
else
{
$ad = 'ad code 2';
}
echo "$ad";
?>