由于每天恶意点击次数过多,我想阻止某些国家/地区访问我的网站。我在这里找到了一个脚本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))
这是否会阻止访问其代码存储在数据库中的所有国家/地区?或者我应该做点别的什么?这个脚本真的有用吗?或者有什么我需要克服的缺陷吗?我对这些问题很困惑。请帮助我...
最佳答案
根据我的经验,应该使用in_array()
使用常见的方法怎么样 if ($two_letter_country_code=="US"|| $two_letter_country_code=="UK")
它有效吗?
关于PHP 按国家/地区阻止网站访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44492042/