Geo Coding for Specific Landing Pages
I was asked yesterday by a website owner if it would be possible to discover where his users were coming from using IP addresses and redirect or show them different content accordingly. I remembered that I had accomplished a similar task only a few months ago using Maxminds geo locating data file to overlay on Google maps showing the location of peoples IP addres and providing reverse look ups of hostnames etc.
Note: if anyone would like this script just leave a comment and I will try and dig it out.
Using the same data file provided by maxmind "GeoLiteCity.dat" and their API "geoipcity.inc" this task was very simple. First we need to include the API and then load the map file.
// API provided for communicating with the .dat database file.
include("geoipcity.inc");
//Load the map file
$gi = geoip_open("GeoLiteCity.dat",GEOIP_STANDARD); // load data file
We then need to get the IP address of our visitor using one of the PHP SERVER functions like so.
//get the user's ip address
$ip = $_SERVER['REMOTE_ADDR'];
//test ip address
//America 74.54.132.180
//UK 213.171.218.144
I have added a few test IP address that you can use to test your script once we have finished. Now passing in the IP address of the visitor and the map file we have already loaded, the API's function "geoip_record_by_addr" will return the visitors location which we save into our record variable.
//get the visitors location
$record = geoip_record_by_addr($gi, $ip);
We can now access the country_name property of our record object and assign it to the yourCountry variable. I then converted the returned string to lowercase for checking later on, I find this can reduce mistakes but is completely optional to your preference.
//assign our country to the your country variable
//and convert it to all lower case
$yourCountry = strtolower($record->country_name);
Finally we have a switch statement to decide what actions we are going to take depending on the location of our visitors. I have only added a few countries but it would be very easy to add more, they are pretty much as you would guess but if you would like to check the country names in the API take a look in the "geoip.inc" file. Of course if you only intend to show the user which country they are from the switch code would not be needed, you could quite easily just "echo $record->country_name".
switch ($yourCountry)
{
case "australia":
echo "You are from Australia";
break;
case "united states":
echo "You are from America";
break;
case "united kingdom":
echo "You are from the UK";
break;
case "japan":
echo "You are from Japan";
break;
case "china":
echo "You are from china";
break;
default:
echo "Show default landing Page";
break;
}
Note: The API also has functions to be a lot more accurate than just returning the country but i will leave that for you to figure out by yourself.
Comments
Yeah great post Ben.. Could you also show us how to integrate the geo coding with Google maps using JQuery?
Thanks.....
Hello Doggz, are you wanting to use JQuery with the Google API and if so what advantages are you trying to achieve over just using JavaScript?
Would using the Jmaps plugin for jquery which allows easy management of google maps be able to achieve what you are after?
Ben
Hi :)
When i read this i know the procedure by getting the details through PHP but usually i used the ip-details.com to get the ip address, location. Now i got an idea to get through those details by using the PHP code
Thanks for your information
Add new comment