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
Geo targeting code
Moderator: General Moderators
Re: Geo targeting code
Did you check the value of the IP you are getting from $_SERVER['REMOTE_ADDR'] ? if you are using a reverse proxy such as nginx, you will need to use other parameters
Also, I'd recommend the Net_GeoIP PEAR package for geolocation (uses the maxmind database as well). It's much more performant and reliable than trying to query the database yourself (it uses a file database by the way)
http://pear.php.net/package/Net_GeoIP/download
Also, I'd recommend the Net_GeoIP PEAR package for geolocation (uses the maxmind database as well). It's much more performant and reliable than trying to query the database yourself (it uses a file database by the way)
http://pear.php.net/package/Net_GeoIP/download
Re: Geo targeting code
pytrin
Thanks for your input.
I'm fairly new to PHP so please bear with me as i'm still trying to get to grips with the language.
Before I try the pear geoip, i think i may have narrowed down the issue slightly.
I think the problem might be to do with where i am placing the php code.
The first part of the code I have been placing in the 'mysql_connect' file which is outside of the root (for security reasons as it contains database password details). The second part of the code I have been placing in the 'header' file, i.e. from:
I commented out the code in the mysql_connect file and it seems that header code is still redirecting which suggests that the header code is running seperate from the mysql_connect code. This therefore may be the problem.
Solutions I have tried include:
1. placing all code within mysql_connect. This does nothing.
2. Placing all code in the header file. For security reasons as mentioned above, I don't think this is advisable.
Am I on the right lines, and can anyone offer any further guidance?
Many thanks again
R
Thanks for your input.
I'm fairly new to PHP so please bear with me as i'm still trying to get to grips with the language.
Before I try the pear geoip, i think i may have narrowed down the issue slightly.
I think the problem might be to do with where i am placing the php code.
The first part of the code I have been placing in the 'mysql_connect' file which is outside of the root (for security reasons as it contains database password details). The second part of the code I have been placing in the 'header' file, i.e. from:
Code: Select all
if($countryCode=='GB'){
echo header('Location: page1.php');
}
else
{
echo header('Location: page2.php');
}
I commented out the code in the mysql_connect file and it seems that header code is still redirecting which suggests that the header code is running seperate from the mysql_connect code. This therefore may be the problem.
Solutions I have tried include:
1. placing all code within mysql_connect. This does nothing.
2. Placing all code in the header file. For security reasons as mentioned above, I don't think this is advisable.
Am I on the right lines, and can anyone offer any further guidance?
Many thanks again
R
Re: Geo targeting code
Problem solved, I think.
Firstly, there was a small, albeit crucial error in the code:
The error line original code was:
This should have read (highlighted correction):
That was the first issue. The second issue was that it seemed that the code would not work from within the mysql_connect file which is located outside the root folder. However, in order for the code to work, the entire code needs to be placed within the one file. This brings about security issues as to place all the code inside a file which is inside the root folder exposes the database password details.
What I did was in order to keep database password info secure, I created a new file and called it 'geo_target.php'. I placed the entire code within this file and I placed the file outside the root folder. I then did a 'require_once' in the header file which called the 'geo_target.php' file.
This now redirects to the correct page based on the user's ip location. It also solves the minor headache I was having in how to protect the database password details.
From my understanding this now gives the database added security as the password details are in a file outside of the root folder. Do you agree?
Many thanks
R
Firstly, there was a small, albeit crucial error in the code:
The error line original code was:
Code: Select all
$res = mysql_query("SELECT COUNTRY_CODE2, COUNTRY_NAME IPCountries WHERE IP_FROM<=inet_aton('$IP') AND IP_TO>=inet_aton('$IP')");//look up IP addressCode: Select all
$res = mysql_query("SELECT COUNTRY_CODE2, COUNTRY_NAME [b][color=#FF0000]FROM[/color] [/b]IPCountries WHERE IP_FROM<=inet_aton('$IP') AND IP_TO>=inet_aton('$IP')");//look up IP addressWhat I did was in order to keep database password info secure, I created a new file and called it 'geo_target.php'. I placed the entire code within this file and I placed the file outside the root folder. I then did a 'require_once' in the header file which called the 'geo_target.php' file.
This now redirects to the correct page based on the user's ip location. It also solves the minor headache I was having in how to protect the database password details.
From my understanding this now gives the database added security as the password details are in a file outside of the root folder. Do you agree?
Many thanks
R