Geo targeting code
Posted: Sat Sep 25, 2010 6:18 am
Hi
I was wondering if you guys could offer some advice on the below code that I'm having problems with. The issue is related to php geo targeting code. The purpose of the code is to redirect a user to a certain page based on the country the user is accessing the internet from.
I will post the code first then describe the problem at the bottom. Just so you know, I have set up a table in mysql with all the relevant fields that are required.
$DatabaseServer = "myserver";
$Username = "myusername";
$Password = "mypassword";
$DatabaseName = "mydatabasename";
$link = mysql_connect($DatabaseServer, $Username, $Password) or die('Could not connect: ' . mysql_error());
mysql_select_db($DatabaseName) or die('Could not select database');
$IP = $_SERVER["REMOTE_ADDR"]; //Get the IP address
$res = mysql_query("SELECT COUNTRY_CODE2, COUNTRY_NAME IPCountries WHERE IP_FROM<=inet_aton('$IP') AND IP_TO>=inet_aton('$IP')");//look up IP address
$Codes = mysql_fetch_array($res); //get result
$CountryCode = $Codes['COUNTRY_CODE2']; //two-letter country code
$CountryName = $Codes['COUNTRY_NAME']; //full country name
echo "$CountryCode - $CountryName"; //print it out, just as an example
mysql_close($link); //clean up
if($countryCode=='GB'){
echo header('Location: page1.php');
}
else
{
echo header('Location: page2.php');
}
I am testing this in the UK with a UK ip address.
Problem:
As I am in the UK with a UK ip address, when I run the test it should take me to 'page 1.php'. However it is not and instead is taking me to 'page2.php'. I have subsequently changed the 'CountryCode' to other countries and have asked friends in those locations to test it also. However, the same problem arises. They are also redirected to 'page2.php'
I've checked to see if it is a problem with the ip database that i am using. However, it does not seem to be this as this is a popular ip database used by many with excellent reviews (maxmind).
So, my question is whether there is something wrong with the code itself which is causing the incorrect redirection?
I hope that all makes sense. And many thanks in advance.
R
I was wondering if you guys could offer some advice on the below code that I'm having problems with. The issue is related to php geo targeting code. The purpose of the code is to redirect a user to a certain page based on the country the user is accessing the internet from.
I will post the code first then describe the problem at the bottom. Just so you know, I have set up a table in mysql with all the relevant fields that are required.
$DatabaseServer = "myserver";
$Username = "myusername";
$Password = "mypassword";
$DatabaseName = "mydatabasename";
$link = mysql_connect($DatabaseServer, $Username, $Password) or die('Could not connect: ' . mysql_error());
mysql_select_db($DatabaseName) or die('Could not select database');
$IP = $_SERVER["REMOTE_ADDR"]; //Get the IP address
$res = mysql_query("SELECT COUNTRY_CODE2, COUNTRY_NAME IPCountries WHERE IP_FROM<=inet_aton('$IP') AND IP_TO>=inet_aton('$IP')");//look up IP address
$Codes = mysql_fetch_array($res); //get result
$CountryCode = $Codes['COUNTRY_CODE2']; //two-letter country code
$CountryName = $Codes['COUNTRY_NAME']; //full country name
echo "$CountryCode - $CountryName"; //print it out, just as an example
mysql_close($link); //clean up
if($countryCode=='GB'){
echo header('Location: page1.php');
}
else
{
echo header('Location: page2.php');
}
I am testing this in the UK with a UK ip address.
Problem:
As I am in the UK with a UK ip address, when I run the test it should take me to 'page 1.php'. However it is not and instead is taking me to 'page2.php'. I have subsequently changed the 'CountryCode' to other countries and have asked friends in those locations to test it also. However, the same problem arises. They are also redirected to 'page2.php'
I've checked to see if it is a problem with the ip database that i am using. However, it does not seem to be this as this is a popular ip database used by many with excellent reviews (maxmind).
So, my question is whether there is something wrong with the code itself which is causing the incorrect redirection?
I hope that all makes sense. And many thanks in advance.
R