How To Do Geo Targeting

Are you looking for a free geo-targeting solution for your ad display? Most affiliate / CPA programs have certain geo targeting requirements, e.g. a UK based company might only prefer UK customers. In that case, you only want to display the appropriate ad to UK visitors. And for other visitors, you want to display a different set of ad.

MaxMind, an industry-leading provider of IP intelligence and online fraud detection tools, offers free IP geolocation databases. Using the GeoLite database, you can perform geo-location lookups of a visitor’s IP address, then you can display different content or redirect visitors based on the country they come from. The GeoLite databases are distributed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. The attribution requirement could be met by simply placing a link back to the MaxMind website.

Here is a simple way to set up geo targeting to display different ads or redirect visitors:

1. Download GeoLite Country Database and When you save the file, do not put any space after the last line ?>, or you will receive an error “PHP Warning: Cannot modify header information – headers already sent by (output started at …”.

2. Put the above files in your website root directory.

3. For example, if you want to redirect a Canadian / UK visitor to somedomainname01, and the rest to somedomainname02, try the following code.

include_once ‘’;
$gi = geoip_open(‘GeoIP.dat’, GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER[‘REMOTE_ADDR’]);
$preferred_countries = array(‘ca’, ‘gb’);
if (in_array(strtolower($country), $preferred_countries)) {
} else {

If you want to display ad-01 to a Canadian / UK visitor, and display ad-02 to the rest, replace header(‘Location:’); with print “…. ad-01 here ….”; etc etc…