Geotargeting with PHP : A complete guide

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.

By Patrick Altoft. at 10:45PM on Wednesday, 05 Sep 2007

Patrick is the Director of Strategy at Branded3 and has spent the last 11 years working on the SEO strategies of some of the UK's largest brands. Patrick’s SEO knowledge and experience is highly regarded by many, and he’s regularly invited to speak at the world’s biggest search conferences and events. Follow Patrick Altoft on Twitter.

comments

20 Responses to “Geotargeting with PHP : A complete guide”

  1. Vittorio says:

    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 says:

    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 says:

    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. [...] – Geotargeting with PHP : A complete guide Does what it says on the tin. Excellent. (tags: links affiliate database script geotargeting webdev [...]

  5. 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.

  6. 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.

  7. Roman says:

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

  8. Bir says:

    [...] block by ip address, deny access by country GeoIP veritaban

  9. Dave says:

    You can import the CSV version of the MaxMind database into MySQL which should help with the resource usage. MySQL is more efficient than having PHP parse a .dat file.

    Of course, saving a cookie for repeat visitors to avoid even the MySQL lookup is also a very good idea.

  10. [...] and perform a conversion on the fly? Here’s a useful blog post that explains how it can be done: Geotargeting with PHP : A complete guide [...]

  11. Dave says:

    @Suzy Turnbull

    Browsers send a language header with every request that is usually based on either the language setting in their OS or the version of the browser that the user downloaded. As you mentioned, choosing a language based on a GeoIP lookup can be fraught with danger. I travel a lot but I still only really know English, even when I’m in Italy or Germany. The language header can also provide fallback languages so if a user indicates that they know Spanish, Portuguese and English in that order, you can try to serve your site in which ever of those languages you have available but you should prefer languages that they know the best.

    My language header currently looks like this, even when I am in another country:

    Accept-Language: en-us,en;q=0.5

    I really should change it to en-gb…

  12. Martin says:

    Okay, so once you install the database to MySQL. How do I only show content to Canadian and American visitors?

  13. Dave says:

    MaxMind provide some sample code on their website: http://www.maxmind.com/app/csv

    I would probably wrap the code they provide in a function with the same name as the one that Patrick used in the example so you can use it like this:

    geoip_country_code_by_addr($gi, $REMOTE_ADDR);

    MaxMind recommend for speed an disk space usage that their binary format and APIs be used rather than importing into MySQL. I’m dubious that this would be faster but since it’s at least two years since I did any benchmarks on this I can’t be sure. Be sure to leave a comment if you do any benchmarking.

    One useful hint if you are importing this into a MySQL database: Don’t forget to make the IP number fields BIGINTs rather than just plain INTs. Otherwise the last 1/3rd of the IP address space will all map to the same location.

  14. webous says:

    I have just released (commercial, but free licenses available) geotargeting plugin for WordPress: http://thewordpress.net/plugins/geotargeting/

    Basically it's just a wrapper around free maxmind database, but with nice wordpress integration: country-based contents, redirects by country, country personalization (country name & flag).

  15. Evan says:

    I used lambda GeoIP (lambdageoip.com), it was $20 and only took one one of code.

  16. Sam says:

    Evan: I use Lambda GeoIP as well. Its pretty awesome for what it does. The crappy part is that its PHP and doesn’t have Javascript built in. If you want to use it in an ASP or Ruby script, you’ll have to hack up some solution.

    Really the main, singly most important thing, is being able to find the nearest city of a certain population. It is imperative to make it believable to the user by not displaying whatever podunk town they happen to live in.

  17. Jan says:

    Very nice. I also wrote an article geotargeting visitors with PHP, MySQL, and the free WEB Net 77 IP-2-Contry database — without using cookies though:

    http://www.heidoc.net/joomla/technology-science/php-and-web-design/62-geolocating-and-geotargeting-your-visitors

    Combining these two should yield a pretty good process.

  18. Frank says:

    Hi,

    I’m interested in geotargeting and would like to ask you two questions: Can this php code work alongside w3 total cache plugin? I was told both are incompatible…
    Also, I want to geotarget countries _and_ cities. Can this be done?

    Thanks in advance!

  19. Frank says:

    …also, have you heard anything from this plugin:
    http://thewordpress.net/plugins/geotargeting/

    The site looks rather fake or like those selling “get millionaire overnight” ebooks, but that’s what I’ve found on google so far. I would appreciate your opinion.

Leave a Reply