Redirecting Users based on User's Country!

4 replies
Sometimes you need to redirect visitors based on the country they come from. Maxmind provides a great country/IP address database in binary format. The following example will show you how redirect visitors from countries which are not on the "whitelist".

Ok, so, just to let you know, this is how I did it:

I don't really have ANY experience with PHP, but I started searching everywhere to find a solution. And this seems to have worked:

First, I downloaded MaxMind's GeoIP.dat database from here:

http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
Free Version (GeoLiteCountry)

Then I downloaded geoip.inc from here:
http://geolite.maxmind.com/download/.../php/geoip.inc

Then I uploaded those two files to the same directory where my page is located.

I edited my php page and wrote this script inside of it

Always make sure to update the GeoLite databases once every month or so.

Here it goes:

PHP Code:
<?php
require_once("geoip.inc");

$gi geoip_open("GeoIP.dat",GEOIP_STANDARD);

$country_code geoip_country_code_by_addr($gi$_SERVER['REMOTE_ADDR']);
geoip_close($gi);

if(
$country_code == 'US'
{
        
header('Location: http://USSite.com');
}
elseif(
$country_code == 'CA')
{
        
header('Location: http://CanadaSite.com');
}
elseif(
$country_code == 'ES')
{
        
header('Location: http://SpainSite.com');
}
else {
        
header('Location: http://AllSite.com');
}
?>
You can alternatively add some HTML Code or an IFRAME like this:

PHP Code:
<?php
 
require_once("geoip.inc");
 
 
$gi geoip_open("GeoIP.dat",GEOIP_STANDARD);
 
 
$country_code geoip_country_code_by_addr($gi$_SERVER['REMOTE_ADDR']);
 
geoip_close($gi);
 
 if(
$country_code == 'US'
 {
         
?> HTML Code US <?php;
 }
 elseif($country_code == 'CA')
 {
         ?> HTML Code CA <?php;
 }
 elseif($country_code == 'ES')
 {
         ?> IFRAME SPAIN <?php;
 }
 else {
         header('Location: http://AllSite.com');
 }
 ?>
You get the idea.

Country Codes can be found here: MaxMind - ISO 3166 Country Codes

Woalah!
Hope this helps!
#based #redirecting #user #users
  • Profile picture of the author cevin
    Thanks Pablo, country redirection is a very common question especially on newbies web-developers.
    Signature
    Service computer at AthensPC. Fast laptop repair, qualified technicians, technical support via internet and online computer lessons.
    {{ DiscussionBoard.errors[3286085].message }}
  • Profile picture of the author PabloVTB
    what do you mean by that optiplex. Could you implement it?
    {{ DiscussionBoard.errors[3292211].message }}
    • Profile picture of the author kidpunky
      hello
      i receive an error while using the code you wrote there , i uploaded the 2 files on the server and i made a redirection ..when people from romania come to the website website.com they are redirected on the romanian language but when people from other country try to visit the website receive 303 eror that is not redirecting propretly ..do you know how i can fix that ?
      {{ DiscussionBoard.errors[4627098].message }}
  • Profile picture of the author Snatch
    Hey thank you for your helpful info. A bit confused, I want to create a iframed cpa offer which if somebody comes from usa goes direcly to a usa offer, uk for uk offer and so forth. How should I structure that page? Should I create a new php page on dreamweaver and throw the code youve provided or how should I start? Sorry about the newbie question but I really need to setup this as soon as possible
    {{ DiscussionBoard.errors[4794725].message }}

Trending Topics