Weekend Projects – GeoIP Country Redirect

Posted on June 6th, 2009

Weekend Project

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.

Posted in Weekend Projects | | | Digg This | del.icio.us | Technorati


Related Topics:
Top 10 Weekend Projects of 2008
Weekend Projects – Social Bookmarking
Weekend Projects – Landing Page
Weekend Projects – Keep It Simple
Weekend Projects: Know your Enemy

RSS feed

2 Comments

Gravatar
Comment by Jacques | Website Design ~ come follow me on twitter @JacquesSnyman
2009-06-07 05:00:18

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.

 
Gravatar
Comment by Fabricio Vasselai Subscribed to comments via email
2009-06-10 17:43:04

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!

 

Sorry, the comment form is closed at this time.

© 2007 BlueFur Hosting | Privacy Policy
Theme by Unique Blog Designs