Thursday, July 5, 2012

Geo Targeting : How to show different content for different geographic locations using PHP

Hi all, today i will show you how to redirect visitors to their country based on their geographic location. Confused? OK let me clear you. For example if a visitor is visiting the site from USA then the content for that targeted location will be displayed to that visitor like if a visitor is from Canada the targeted content for Canada will be displayed to them and so on. Wow!! it sounds interesting. Isn't it? Yes of course. This feature is very much important in advertising a content for a specific geographic location. So this is the technology we call it as Geo targeting.

So lets see what is Geo targeting?

Geo targeting is a technology which displays website content for a specified targeted location. For example the visitors who are visiting the website from their location say India the respective content for India will be displayed to the Indian visitors. Likewise, for different countries users, different content will be shown to them. This technology is best for online advertising for small to large enterprises to earn more revenue. The best example for Geo targeting is Google's DFP(Double Click for Publishers).

So where this Geo targeting is going to happen?

Generally Geo Targeting happens at the server side, means whenever a visitor is requesting the website content from their browser it sends the request to the server and the server checks the visitors ip address with the stored ip addresses in the database server. If a specific match is found for a specific country then the content targeted for that country will be shown to the visitor.

So why this Geo Targeting is important for me?

If you want to publish the ad to the targeted location then i suggest you to use this technology.

Now lets get our hands dirty..

The tools to be used for Geo targeting:

1. Any server side programming language. In this case i am going to use PHP.
2. MySQL IP Address Database. This database contains at around 55000 records. So the retrival of results will be some what slower for high traffic website. So do notice this change.

How to get the IP Addresses Database or Where can i find the IP Addresses Database?

You can find here

Here you can find in the form of a text file between ip ranges. For different countries different ranges will be given. We need to convert it to one table then only we can use this database. To use this database you need to convert it text to csv to sql format. So its a time taking process. But every month they will update their database of IP addresses. Every month we need to update our database also.

You can find here

Maxmind provides free IP address database in the csv format. Almost it provides 95% accurate results of IP addresses. But to use this database you have to covert it to sql format then import it to the database server.

But here i am providing php as well as IP address database for free. You can download the entire project.

How i stored the different IP Addresses in the database?

Generally all the countries will have different IP addresses, in this tutorial i am converting the IP Address into IP Number. Here IP Number is nothing but an IP Address in the 32 bit decimal notation. IP address (IPv4 / IPv6) is divided into 4 sub-blocks. Each sub-block has a different weight number each powered by 256. IP number is being used in the database because it is efficient to search between a range of number in database. I will show you how to convert an ip address into ip number.

Let us assume an ip number

122 x 2563  + 175 x 2562  + 35 x 2561  + 118 x 2560  = 2058298230 (IP Number)

Manually if you want to store the ip numbers in the database then follow the above procedure. Other wise go for any of the available ip address databases. Once this process is over upload the database in the database server.

Note : I have a ip address database with 2 columns only namely ip number and country code.

Now lets see the code:

Did You Enjoy this Article ?

If yes, Then enter your email below to get

more such great articles in your inbox

For FREE !


  1. Nice, for a free approach. I like the MaxMind data because it is stored in binary format (and updated monthly). There is a free version of their data is highly accurate, so you don't need the free copy. I'd like to see you go deeper with this and allow targeting at the city level, just 2 variables away :)

    1. Thanks for reading and commenting keith..let me try for the city level..thanks for your suggestion..

  2. Can we use long. n lats. for100% accuracy in geo targetting ? if so than, can u pls tell me steps sequentially...........n thanks for this great article.

  3. Hello,

    You guys should try Geolify which is a web software for creating and delivering geo targeted content to your website. You don't need to do any coding or database creation.

    Just upload / create your content in their web software, select the visitor location and you will get one line of java code to paste into your website's html where you need the content to appear

    check it out

  4. can you use geo-targetting for different places in small countries. please help i have site and i want it to display ads according to specific local areas

    1. You just need to get a geolocation database with granularity of IP to city level. Free webservice such as or free database at shall give you the solution.

      However, I have to point out the city level may not be highly accurate as compare to the country level, as it very much depend on how the ISP allot the IP range locally.

  5. Can I suggest the inclusion of free IP2Location LITE database ( as an alternative to maxmind?


Thank you for reading and commenting...