Redirecting Users based on Different Countries - Cyber Tweaks

Ads Top

Redirecting Users based on Different Countries

Content is free. When you buy through links on my site, I may earn affiliate commission. Learn More.

Redirect user to his country php script

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 to redirect visitors from countries which are not on the "whitelist".

This script is useful in many ways. if you are working on Affiliate networks then you can use it to redirect your users to different offers according to his country.

You required to upload a total of 3 files.

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

https://dev.maxmind.com/geoip/geoip2/geolite2/

Second, Download geoip.inc from here:

https://www.mediafire.com/?2w4jo2ydbywa675

Third, create redirect.php check 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') 
{
        header('Location: http://google.us');
}
elseif($country_code == 'CA')
{
        header('Location: http://facebook.com');
}
elseif($country_code == 'ES')
{
        header('Location: http://google.es');
}
else {
        header('Location: http://google.com');
}
?>

Find out country codes from here : https://www.maxmind.com/en/iso3166

Download full source with samples from here : https://www.mediafire.com/?04m0ymk6cnwbyim

No comments:

Powered by Blogger.