I like blogs that teach me something. With that in mind, I wanted to pass something on which might be useful for my readers. So here it is, a how to guide on developing a page which will redirect the visitor to a different page depending on where they are in the world. My site is built in PHP so that's what I'll be describing. Although it would be great to have a site for every country, I've developed one for the two countries where I live: the UK and the US.
There are two parts to the detection, 1 is the php page and the other is a database. To get the data for the database go here: http://ip-to-country.webhosting.info/node/view/6 and click on the CSV download. It's updated every month or so. Then comes the PHP.
The code is below, if you replace the myhost, mydata, mypassword, mydatabase and the web pages you want to redirect to then you should have a fully functional ip to country redirection.
// Replace this MYSQL server variables with actual configuration
$mysql_server = "myhost";
$mysql_user_name = "mydata";
$mysql_user_pass = "mypassord";
// Retrieve visitor IP address from server variable REMOTE_ADDR
$ipaddress = getenv(REMOTE_ADDR);
// Convert IP address to IP number for querying database
$ipno = abs(ip2long($ipaddress));
// Connect to the database server
$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
// Connect to the IP2Location database
mysql_select_db("mydatabase") or die("Could not select database");
// SQL query string to match the recordset that the IP number fall between the valid range
$query = "SELECT * FROM IPCountry WHERE $ipno <= ipTO AND $ipno >= ipFROM";
// Execute SQL query
$result = mysql_query($query) or die("IP2Location Query Failed");
// Retrieve the recordset (only one)
$row = mysql_fetch_object($result);
// Keep the country information into a variable
$CountryShort = $row->CountryShort;
// Free recordset and close database connection
mysql_free_result($result); mysql_close($link);
// If the visitors are from GB, redirect them to GB site
if ($CountryShort == "GB")
{
Header("Location: http://www.1lessjob.com/UK/");
}
elseif ($CountryShort == "US")
{
Header("Location: http://www.1lessjob.com/US/");
}
else
{
// Otherwise, redirect them to splash page
Header("Location: http://www.1lessjob.com/pick.html");
}
exit;
?>
In addition to those already mentioned thanks to the sites (http://us2.php.net/manual/en/function.ip2long.php and https://www.ip2location.com/articles/article2.htm) that helped me build my PHP ip to country detect/redirect.
This concludes my first blog post, hope you find it useful.
Subscribe to:
Post Comments (Atom)
Hi Tom:
ReplyDeleteYou are such a geek that I think the next time you go drinking at the Barking Spider you should sit at the geek table, instead of with those "other people".
I'm impressed by anybody who has the patience to read programming instructions and have the spirit to give it a whirl.
You should put a link on this blog to your 1lessjob site somewhere prominently.