【发布时间】:2017-06-12 05:36:29
【问题描述】:
由于每天都有过多的恶意点击,我想阻止某些国家/地区访问我的网站。我在这里找到了一个脚本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))
这是否会阻止访问其代码存储在数据库中的所有国家/地区?还是我应该做点别的?这个脚本真的有用吗,还是有什么我需要克服的缺陷?我对这些问题感到困惑。请帮帮我...
【问题讨论】: