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 ipaddresslocation.org
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.com
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 22.214.171.124
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 !