Script that only shows content to certain countries?

Discussion in 'White Hat SEO' started by chocolatemonkey, Feb 4, 2011.

  1. chocolatemonkey

    chocolatemonkey Junior Member

    Joined:
    Dec 20, 2009
    Messages:
    197
    Likes Received:
    29
    I need a script that only shows certain content on my site (in my case, a link) if the visitor is from the US and does not show it to anyone that doesn't have a US IP address. I know there is a script out there that can do this, but I don't know how to find one - I don't know what it would be called so I don't know what to g00gle.

    Whoever helps me find one (or makes one if they wish) will get +repped, since I really need this!

    Thanks in advance! :)
     
  2. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,868
    Likes Received:
    1,336
    You can search for "ip localization." You could perform a PHP cURL POST to a free IP lookup service such as IP2Location or GeoBytes, or you could use a service that provides an API such as MaxMind. I also think GODcpa does some sort of geo-targeting but I'm unsure of how well it works as I have not used it.
     
  3. BugFixed

    BugFixed Junior Member

    Joined:
    Sep 24, 2010
    Messages:
    130
    Likes Received:
    40
    myfunctions.php
    Code:
    <?php
    include "geoip.inc";
    
    function GetUserIP() 
    {
    	if (isset($_SERVER)) {
           if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
             return $_SERVER["HTTP_X_FORWARDED_FOR"];
           if (isset($_SERVER["HTTP_CLIENT_IP"]))
                return $_SERVER["HTTP_CLIENT_IP"];
            return $_SERVER["REMOTE_ADDR"];
        }
        if (getenv('HTTP_X_FORWARDED_FOR'))
            return getenv('HTTP_X_FORWARDED_FOR');
        if (getenv('HTTP_CLIENT_IP'))
            return getenv('HTTP_CLIENT_IP');
        return getenv('REMOTE_ADDR');
    }
    
    function get_country_id() 
    {
    	global $config;
    	$realip = GetUserIP();
    	$handle = geoip_open("GeoIP.dat", GEOIP_STANDARD);
    	$co = geoip_country_id_by_addr($handle, "$realip");
    	geoip_close($handle);
    	return $co;
    }
    
    ?>
    
    Usage, test.php
    Code:
    <?php
    include "myfunctions.php"
    if (get_country_id()=="US"){
      //redirect to US specific location
      echo "the visitors should be came from US";
    } else {
    echo "from other country!, weird";
    }
    ?>
    You need Maximind GeoIP country, though.
    
     
  4. chocolatemonkey

    chocolatemonkey Junior Member

    Joined:
    Dec 20, 2009
    Messages:
    197
    Likes Received:
    29
    Thanks for your help guys, but I haven't a clue how to set those up...

    I tried this script from a member from BHW, but when I tested it with a Brazil proxy, it still took me to the US offer. :confused:

    I got it from his thread: http://www.blackhatworld.com/blackhat-seo/making-money/92299-simplest-geo-redirect-code.html

    Any idea how to modify this guy's script so it redirects correctly?

    HTML:
    <script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
    
    
    <script language="JavaScript">
    
    var country= geoip_country_code();
    
    if(country  == "US")      
    {
    <!--
    window.location = "http://link-for-US-visitors.com/"
    //-->
    }
    
    else   
    {
    <!--
    window.location = "http://lnik-for-non-US.com"
    //-->
    }
    
    </script>
    I feel like the "else" part is causing problems, but I don't know if that's true or not...

    I even modified the "else" part to
    HTML:
    if(country  == "BR")  
    and it still took me to the US offer...:confused:
     
    Last edited: Feb 4, 2011
  5. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,868
    Likes Received:
    1,336
    Are you sure your proxy is based in Brazil? Visit that Javascript file that you're including in the page and tell me what the geoip_country_code() is returning. The other functions in that file also return state, city, etc. It's working fine for me as it found my location as my exact city, area code, etc.

    http://j.maxmind.com/app/geoip.js

    If you want, also put the IP into these sites:
    http://www.ip2location.com/free.asp
    http://www.geobytes.com/iplocator.htm

    and see where it says your Brazil IP is really from.
     
    • Thanks Thanks x 1
  6. chocolatemonkey

    chocolatemonkey Junior Member

    Joined:
    Dec 20, 2009
    Messages:
    197
    Likes Received:
    29
    Maxmind and Geobytes check out as my actual IP, but ip2location says brazil, so I guess Maxmind and Geobytes can tell when you're using a proxy, which is pretty cool.

    Thanks for your help!