PHP按国家/地区阻止网站访问

时间:2017-06-12 05:36:30

标签: php web access blocking

我想阻止一些国家/地区访问我的网站,因为每天都有过多的恶意攻击。我在这里找到了一个脚本http://azuliadesigns.com/blocking-website-access-country-php/,我可以阻止一个国家​​访问该网站。开始这似乎没问题。但在这里,我只能使用if($two_letter_country_code=="US")阻止一个国家​​/地区。

脚本

if ($_SERVER['HTTP_X_FORWARDED_FOR'])
  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
  $ip   = $_SERVER['REMOTE_ADDR'];

$two_letter_country_code=iptocountry($ip);

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 ($two_letter_country_code=="US")
  die();

我的问题是,如果我想阻止从数据库访问多个国家/地区该怎么办?假设,我已经在我的数据库中保存了一些国家代码来阻止他们说US,IN,PK ....等等,我想阻止访问所有这些代码。在这种情况下,如何修改此脚本才能工作?

我认为应该有效:

$country_codes_to_block = $row['block_countries']; // Fetched from database. This will give me the country codes US,IN,PK... etc.

现在修改此行

if ($two_letter_country_code=="US")

if (in_array($two_letter_country_code, $country_codes_to_block))

这会阻止访问存储在数据库中的代码所在的所有国家/地区吗?或者我应该做些什么?这个脚本真的很有用,还是有任何我需要克服的缺陷?我对这些问题感到困惑。请帮帮我......

2 个答案:

答案 0 :(得分:0)

if ($_SERVER['HTTP_X_FORWARDED_FOR'])
  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
  $ip   = $_SERVER['REMOTE_ADDR'];

$two_letter_country_code=iptocountry($ip);

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";
  }


error_reporting(E_ERROR | E_PARSE);

include ("mysqli_connection_file.php");
$query = "SELECT column_name FROM table_name";
$result = mysqli_query($conn, $query);

if (!$conn) { die("No Connection was made"); }

while($row = mysqli_fetch_assoc($result)) {

    $ccode = $row['name'];

}

    foreach ($ranges as $key => $value) {
      $countryset = $ranges[$key][1];
        //echo $countryset."<br />";

        if($ccode == $countryset) {
            $time = date('Y-m-d H:i');
            $insert = "INSERT IGNORE INTO logged_table VALUES ('','$ip','$time','IP Blocked','$countryset')";

                if ($conn->query($insert) === TRUE) {} else {
                    echo "Error: " . $sql . "<br>" . $conn->error;
                    die;
                }

            mysqli_free_result($result);
            mysqli_close($conn);


            echo "<script>alert('Please take a moment to review a very special offer we are running before you review!')</script>";
            echo "<META http-equiv='refresh' content='0; URL=http://www.leapfrog.com'>";

        }

    }
}

答案 1 :(得分:-1)

在我的经验中,它应该通过使用in_array()

来解决

如何使用像if ($two_letter_country_code=="US" || $two_letter_country_code=="UK" )这样的常用方法呢?