Allow only from one country

Discussion in 'PHP' started by Tom12361, Aug 30, 2009.

  1. #1
    Hi,
    How can allow to use my site only from one country?
     
    Tom12361, Aug 30, 2009 IP
  2. Tom12361

    Tom12361 Active Member

    Messages:
    442
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #2
    I understand that, but where can I get all ip addresses that belong to my country?
     
    Tom12361, Aug 30, 2009 IP
  3. dannywwww

    dannywwww Well-Known Member

    Messages:
    804
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #3
    You could do it by using a geo IP api tool..eg

    http://api.hostip.info/country.php?ip=0.0.0.0

    
    <?php
    $content = file_get_contents('http://api.hostip.info/country.php?ip='.$_SERVER['REMOTE_ADDR']);
    
    if($content != 'UK'){
    header('location: notallowedcountry.html');
    exit;
    }
    ?>
    
    PHP:
    (not tested)
     
    dannywwww, Aug 30, 2009 IP
  4. tulerpeton

    tulerpeton Peon

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Because it can be slow to get query another host ("hostip.info" for example) you can download and use the GEOIP PHP Api ( from maxmind.com) directly from your website... It look like this:

    <?php
    // This code demonstrates how to lookup the country by IP Address

    include("geoip.inc");

    $gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
    $country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
    geoip_close($gi);

    if($country == "UK") /* If UK people don't deserve your website, do they deserve anything? ;-) */
    {
    header('Location: notallowedcountry.html');
    }
    ?>
     
    tulerpeton, Aug 30, 2009 IP
  5. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #5
    tulerpeton is speaking about maxmind.com solution (you get GeoIP.dat and geoip.inc from there)
     
    crivion, Aug 30, 2009 IP
  6. picos

    picos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    81
    #6
    it is impossible, if you want to do it, you have to update IP list-> pay for it, of course. All IP address of client will be reseted so how can u find where IP come from.
     
    picos, Aug 31, 2009 IP