1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Script to Redirect USA users

Discussion in 'HTML & JavaScript' started by philly3, Oct 17, 2011.

  1. philly3

    philly3 Regular Member

    Joined:
    Nov 17, 2010
    Messages:
    436
    Likes Received:
    276
    Gender:
    Male
    Does anyone have a javascript or HTML that will redirect USA visitors to a different site?
     
  2. philly3

    philly3 Regular Member

    Joined:
    Nov 17, 2010
    Messages:
    436
    Likes Received:
    276
    Gender:
    Male
    Bump....
     
  3. jutt29

    jutt29 Regular Member

    Joined:
    Sep 10, 2010
    Messages:
    308
    Likes Received:
    63
    Occupation:
    Student
    Location:
    Internet!
    Do you need it still?
     
  4. appman360

    appman360 Newbie

    Joined:
    Sep 21, 2011
    Messages:
    12
    Likes Received:
    5
    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.
     
  5. bewithme465

    bewithme465 Regular Member

    Joined:
    Aug 24, 2010
    Messages:
    455
    Likes Received:
    91
    Occupation:
    self employee
    Location:
    center of eath
    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
     
  6. The Doctor

    The Doctor Jr. VIP Jr. VIP

    Joined:
    Dec 18, 2010
    Messages:
    612
    Likes Received:
    188
    Occupation:
    Computer Scientist, Engineer, Programmer.
    Location:
    ☆☆☆☆☆☆
    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");
    }
    ?>
     
    • Thanks Thanks x 2
    Last edited: Oct 19, 2011
  7. saxgod

    saxgod Regular Member

    Joined:
    Sep 19, 2010
    Messages:
    351
    Likes Received:
    337
    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 Thanks x 2
  8. philly3

    philly3 Regular Member

    Joined:
    Nov 17, 2010
    Messages:
    436
    Likes Received:
    276
    Gender:
    Male
    Sweet thank you very much!!!
     
  9. saxgod

    saxgod Regular Member

    Joined:
    Sep 19, 2010
    Messages:
    351
    Likes Received:
    337
    Lol seems we wrote the code at the same time :)

    Both will work
     
    • Thanks Thanks x 1
  10. The Doctor

    The Doctor Jr. VIP Jr. VIP

    Joined:
    Dec 18, 2010
    Messages:
    612
    Likes Received:
    188
    Occupation:
    Computer Scientist, Engineer, Programmer.
    Location:
    ☆☆☆☆☆☆
    you're welcome. make sure you put the code at the top of the file before anything else.
     
  11. philly3

    philly3 Regular Member

    Joined:
    Nov 17, 2010
    Messages:
    436
    Likes Received:
    276
    Gender:
    Male
    Thanks/+rep to you both :)
     
    • Thanks Thanks x 2
  12. philly3

    philly3 Regular Member

    Joined:
    Nov 17, 2010
    Messages:
    436
    Likes Received:
    276
    Gender:
    Male
    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 :)
     
  13. hazephase

    hazephase Jr. VIP Jr. VIP

    Joined:
    May 29, 2009
    Messages:
    1,620
    Likes Received:
    93


    Thanks for your info.. its useful for me also :)
     
  14. EisaG

    EisaG BANNED BANNED

    Joined:
    Feb 7, 2011
    Messages:
    208
    Likes Received:
    31
    this isn't working for me
    nvm
     
    Last edited: Feb 14, 2012