
Last week we created Landing Pages for our top 3 inbound links to shape the actions of our traffic. If you did not do this then I suggest you go back and see how to do it now. This week we will look at how to push traffic from countries to a specific page or site. This can be useful if you want certain country prices to be shown or if you have only certain services that are available in specific countries.
To do this project we will be using the MaxMind GeoIP Country Database. You can purchase the IP database for $20 a month and can access their database through an API.
After you have purchased the GeoIP Web Service from MaxMind you can use the following code to redirect for a specific country…
<?php
$ipaddress = getenv(“REMOTE_ADDR”);
$license_key = “yourlicensekey”;
$query = “http://geoip1.maxmind.com/a?l=” . $license_key . “&i=” . $ipaddress;
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . “?” . $url["query"];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout);
if ($fp) {
 fputs ($fp, “GET $path HTTP/1.0\nHost: ” . $host . “\n\n”);
 while (!feof($fp)) {
   $buf .= fgets($fp, 128);
 }
 $lines = split(“\n”, $buf);
 $country = $lines[count($lines)-1];
 fclose($fp);
} else {
 # enter error handing code here
}
if ($country==’CA’)
{
header(“Location: http://www.bluefur.com/canada/”);
exit;
}
else
{
header(“Location: http://www.bluefur.com/index2.php”);
exit;
}
?>
Place the above code into your main php index page (index.php). You will need to move your current index.php page to index2.php for this script to work.
If you get stuck let me know in the comments.





Jacques | Website Design
June 7, 2009 5:00 am
Definitely the way to go if you’re running an international site, with country specific services and / or products. As a South African with bigger internet aspirations this will definitely come in handy. Much obliged.
Fabricio Vasselai
June 10, 2009 5:43 pm
Dear,
And what if I use the free GeoIP version? Should it be the same code?
In this code, then how to enter location of both GeoIP.dat and geoip.inc?
Thanks a lot!