0

I am using a script which I found at http://www.phptutorial.info/iptocountry/the_script.html

This script as you can see allows us to block a country by identifying their IP address.

The complete script is

  $website_mode = "live"; // Set this to live to let the country blocking code work

  function iptocountry($ip){
    $numbers = explode( ".", $ip);
    include("ip_files/".$numbers[0].".php");
    $code = ($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
    foreach($ranges as $key => $value){
      if($key<=$code){
        if($ranges[$key][0]>=$code){
          $country=$ranges[$key][1];
          break;
        }
      }
    }
    if($country==""){
      $country="unknown";
    }
    return $country;
  }

  if($website_mode == "live"){
    $gbc = $pdo->prepare("SELECT bc_countries FROM blocked_countries");
    $gbc-> execute();
    $gbf = $gbc->fetch();
      $countries_to_block = explode(',', $gbf['bc_countries']);

    if($_SERVER['HTTP_X_FORWARDED_FOR']){
      $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
      $ip   = $_SERVER['REMOTE_ADDR'];
    }
    $two_letter_country_code = iptocountry($ip);

    if(in_array($two_letter_country_code, $countries_to_block)){
      die("<center style='margin-top: 150px'><img src='images/stop.png' width='auto' height='200px'><br><br>*** Your country has been banned from accessing this website for security reasons ***</center>");
    }
  }

Here, I have written a script to work with the code which I found on http://azuliadesigns.com/blocking-website-access-country-php/. I modified it a bit so that it works with my database. But on live website its returning the following error.

Warning: include(ip_files/2405:204:4008:c78:d485:aba6:1eb4:9d6c, 2405:204:4008:c78:d485:aba6:1eb4:9d6c.php): failed to open stream: No such file or directory in /storage/supergens5cfqqgc/files/redd.supergenscript.com/functions.php on line 11

Warning: include(ip_files/2405:204:4008:c78:d485:aba6:1eb4:9d6c, 2405:204:4008:c78:d485:aba6:1eb4:9d6c.php): failed to open stream: No such file or directory in /storage/supergens5cfqqgc/files/redd.supergenscript.com/functions.php on line 11

Warning: include(): Failed opening 'ip_files/2405:204:4008:c78:d485:aba6:1eb4:9d6c, 2405:204:4008:c78:d485:aba6:1eb4:9d6c.php' for inclusion (include_path='.:/opt/php-fpm-5.6.29-1/lib/php') in /storage/supergens5cfqqgc/files/redd.supergenscript.com/functions.php on line 11

Warning: Invalid argument supplied for foreach() in /storage/supergens5cfqqgc/files/redd.supergenscript.com/functions.php on line 13

I have just followed all the procedures properly. Also, I have rechecked all the code a lot of times but I am not sure what is causing this error.

  • This is a IPv6 format. Are you expecting IPv4 instead? – Felippe Duarte Aug 15 '17 at 21:57
  • What @Felippe said, plus on Linux, `:` colons cannot appear in filenames, so it is not a wonder they cannot be read or written. You could swap them for underscores, or some other character that is legal in filenames. – halfer Aug 15 '17 at 21:58
  • @FelippeDuarte I am expecting 101.102.103.104 format.. IPv4 format.. how can I convert it to IPv4 format? And will that be solving the problem? –  Aug 15 '17 at 21:59
  • 1
    dont be mean, why block countries? (and its usually trivial to get around if you want to) –  Aug 15 '17 at 22:04
  • Hmm, even if you swap the colons for something else, it looks like it is using the IPv4 format to look up country information from a directory structure. That is not going to work with IPv6 unless you have country definition files for that as well. – halfer Aug 15 '17 at 22:04
  • I'm pretty sure you can't convert to IPv4. IPv6 appear because IPv4 has a limited available addresses. What you can do here is to have a country list by IPv6, or ignore when you get a IPv6 number, or get IP from a different $_SERVER variable. In some cases you can convert back, this post explains better: https://stackoverflow.com/questions/2786632/how-can-i-convert-ipv6-address-to-ipv4-address – Felippe Duarte Aug 15 '17 at 22:04
  • Is your IPv6 address coming from `$_SERVER['HTTP_X_FORWARDED_FOR']` or `$_SERVER['REMOTE_ADDR']`? – halfer Aug 15 '17 at 22:04
  • @halfer when I echoed `$_SERVER['REMOTE_ADDR']` it gave `2405:204:4008:c78:d485:aba6:1eb4:9d6c` and when I echoed `$_SERVER['HTTP_X_FORWARDED_FOR']` it gave `2405:204:4008:c78:d485:aba6:1eb4:9d6c, 2405:204:4008:c78:d485:aba6:1eb4:9d6c`. Hence, both are returning IPv6 addresses. What to do now? I want IPv4 addresses only. –  Aug 15 '17 at 22:18
  • OK, so I guess you're on part of the internet that is wholly IPv6. That will of course be the case for everyone one day! Unfortunately that means your IPv4 script (and country data files) are of no use to you (they may be useful for people who reach your server via IPv4 though). You will need to find an IPv6 database, [e.g. here](https://stackoverflow.com/questions/9060895/ip-to-country-ipv6). – halfer Aug 15 '17 at 23:44
  • 1
    @halfer I already googled a lot for IPv6 database but could not find any... :( –  Aug 15 '17 at 23:48
  • How many queries a day are you expecting? On the link I provided, there is an API that will let you do 1,000 queries/day free of charge - and that supports IPv6. – halfer Aug 16 '17 at 00:40

0 Answers0