Script to Redirect USA users

philly3

BANNED
Joined
Nov 17, 2010
Messages
544
Reaction score
305
Does anyone have a javascript or HTML that will redirect USA visitors to a different site?
 
It's easier and faster to do this in a server side language (php, asp) using GeoIP.

I can't post a link, but, if you google 'YQL Geo Library' you'll find a javascript one that detects the location.
 
interesting topic is this possible i never tried it,
one thing clarify me are u asking directly usa visitors
or you got usa visitor and want to redirect him to another website
plz be specific
 
I have decided to write the code for you. You will need to make sure your file is a .php file for this to work. You will also need to sign up for a free API key at ipinfodb.com. You will need to add your API key and the URL to redirect to to this code and then add this code to the top of your file:
PHP:
<?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");
}
?>
 
Last edited:
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();
}
 
Sweet thank you very much!!!
 
Worked great by the way thanks again guys. Just put it on my site and worked flawlessly and only took about 5 minutes to do :)
 
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();
}



Thanks for your info.. its useful for me also :)
 
Back
Top