<?php
function GetBetween($content,$start,$end)
{
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
$redirectURL = ""; // put URL to redirect to between quotes.
$apiKey = ""; // put your ipinfodb.com API key between quotes.
$ip = $_SERVER['REMOTE_ADDR'];
$userIpLocation = @file("http://api.ipinfodb.com/v2/ip_query.php?key=$apiKey&ip=$ip&timezone=false");
$userCountry = GetBetween($userIpLocation[4],"<CountryName>","</CountryName>");
if($userCountry == "United States")
{
header("Location: $redirectURL");
}
?>
<?php
require_once("geoip.inc");
$ip = $_SERVER['REMOTE_ADDR'];
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$cc = geoip_country_code_by_addr($gi, $ip);
if ($cc == "US") {
header("Location: http://www.yourUStarget.com");
die();
} else {
header("Location: http://www.restOfTheWorld.com");
die();
}
Lol seems we wrote the code at the same timeI have decided to write the code for you.
Sweet thank you very much!!!
Get the GEOLITE db from http://www.maxmind.com/app/geoip_country
Also get the GEOIP PHP library here: http://geolite.maxmind.com/download/geoip/api/php/geoip.inc
Place the files geoip.dat (from the first url) and geoip.inc (from the second url) in the folder of you webroot on your webserver (like the public_html dir)
Now use the following script:
PHP:<?php require_once("geoip.inc"); $ip = $_SERVER['REMOTE_ADDR']; $gi = geoip_open("GeoIP.dat",GEOIP_STANDARD); $cc = geoip_country_code_by_addr($gi, $ip); if ($cc == "US") { header("Location: http://www.yourUStarget.com"); die(); } else { header("Location: http://www.restOfTheWorld.com"); die(); }