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

Detect country and show content script

Discussion in 'PHP & Perl' started by david120, Mar 12, 2012.

  1. david120

    david120 Newbie

    Joined:
    Jan 2, 2011
    Messages:
    33
    Likes Received:
    8
    Location:
    United Kingdom
    I created this script for a couple of my blogs which use a content locker, I wanted to switch my content locker to "Content lock pro" to try and cut out the middle man and make more money.

    Lets just say I'm not exactly using the locker legitimatley and I'm going against these networks TOS. So I created a simple script to allows you to detect the users country and show content only to that country. So I would only show content lock pro to users orginating from the US and all other countries would get a default leadbolt content locker.

    Change <API KEY> to the Ipinfodb's api key. You can sign up for a free api key at Ipinfodb.com

    You can also change "UNITED STATES" to other countries such as "UNITED KINGDOM."

    PHP:
    <?php
    $location 
    $_SERVER['REMOTE_ADDR'];
    $ipp simplexml_load_file("http://api.ipinfodb.com/v3/ip-country/?key=<API KEY>&ip=$location&format=xml");
    $country $ipp->countryName;
        
    if (
    $country == "UNITED STATES") { ?>

    <!-- ONLY DISPLAY CONTENT TO US USERS -->

    <?php } else { ?>
        
    <!--Every other country will see this content -->

    <?php ?>
     
  2. hardeep

    hardeep Newbie

    Joined:
    May 22, 2010
    Messages:
    10
    Likes Received:
    26
    This is awesome, this will be so useful.
    The registration to get the api key was quick and simple.
    Thanks
     
  3. digall

    digall Junior Member

    Joined:
    Feb 3, 2012
    Messages:
    135
    Likes Received:
    13
    This will be nice if someone make this as wp plugin.