<?
// Add a new definition for each new country you need a redirect for.
// Copy the code below and change "COUNTRYCODE" to the country you want.
// All country codes can be found in countrycodes.txt.
// Paste the code on the next row below all other definitions without the "//".
// define( COUNTRYCODE_VISITORS, "http://www.ANY-URL-YOU-WANT.com" );
// After you have added a new definition, scroll down to the bottom and add a new case.
// You can remove all rows starting with "//" if you want.
// US = United States
define( US_VISITORS, "http://www.yoursite.com/us" );
// GB = Great Britain
define( GB_VISITORS, "http://www.yoursite.com/gb" );
// CA = Canada
define( CA_VISITORS, "http://www.affiliate-link-for-canadians-etc.com" );
// Anyone who is not defined will be sent to your catchall URL.
define( CATCH_ALL_VISITORS, "http://www.yoursite.com/catchall" );
if( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
$ip = $_SERVER['REMOTE_ADDR'];
function iptocountry($ip){
$country_code = "";
$arr = explode( ".", $ip );
if( is_file( $_SERVER["DOCUMENT_ROOT"] . "/ip_files/" . $arr[0] . ".php" ) ) {
include( $_SERVER["DOCUMENT_ROOT"] . "/ip_files/" . $arr[0] . ".php" );
$code = $arr[0] *16777216 + $arr[1] * 65536 + $arr[2] * 256 + $arr[3];
foreach( $ranges as $key=>$value ) {
if( $key <= $code ){
if( $value[0] >= $code ) {
$country_code = $value[1];
break;
}
}
}
}
return $country_code;
}
$country_code = iptocountry( $ip );
// Add a new case for each country. Copy the code below and change COUNTRYCODE to the country you want.
// case "COUNTRYCODE":
// header( "Location: ".COUNTRYCODE_VISITORS );
// exit();
// There is no specific order needed, so just add it between
// "switch( $country_code )" and the closing tag "}".
// It's pretty easy. Just follow the pattern below.
// You can't have a case without having a definition though.
// Then the visitor from that country will get an error.
switch( $country_code ) {
case "US":
header( "Location: ".US_VISITORS );
exit();
break;
case "CA":
header( "Location: ".CA_VISITORS );
exit();
break;
case "GB":
header( "Location: ".GB_VISITORS );
exit();
break;
}
// The following code is sending all visitors that has no case and no
// definition to your catchall URL.
// IMPORTANT! If you don't have a case for US traffic they will stay on the landing page.
// You can put HTML below this script, then people from the US will see that page instead
// of being redirected.
// You can change "US" to any other country and it will work the same way.
if( $country_code != "US" ) {
header( "Location: ".CATCH_ALL_VISITORS );
exit();
}
?>