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:
Redirecting Users based on User's Country!
8
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:
You can alternatively add some HTML Code or an IFRAME like this:
You get the idea.
Country Codes can be found here: MaxMind - ISO 3166 Country Codes
Woalah!
Hope this helps!
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');
}
?> 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');
}
?> Country Codes can be found here: MaxMind - ISO 3166 Country Codes
Woalah!
Hope this helps!
- cevin
- PabloVTB
- [1] reply
- kidpunky
- Snatch
Next Topics on Trending Feed
-
8