在我的网站上用PHP阻止特定的IP块 [英] Block specific IP block from my website in PHP
问题描述
我的服务器上没有
.htaccess
文件,所以我必须用PHP来做。 if($ _SERVER ['REMOTE_ADDR'] ==89.95.25.37)die();
会阻止特定IP。如何阻止整个IP块?
非常感谢。
尝试 strpos()
if(strpos($ _ SERVER) ['REMOTE_ADDR'],89.95)=== 0)
{
die();
}
如果你注意到, ===
运算符确保 89.95
位于IP地址的开始。这意味着您可以根据需要分配尽可能多的IP地址,并且无论后面的数字是什么,它都会阻止。
例如,所有这些都将被阻止:
89.95 - > 89.95.12.34
, 89.95.1234.1
, 89.95.1.1
89.95.6 - > 89.95.65.34
, 89.95.61.1
, 89.95.6987
(其中一些不是有效的IP地址)
I'd like, for example, block every IP from base 89.95 (89.95..).
I don't have .htaccess
files on my server, so I'll have to do it with PHP.
if ($_SERVER['REMOTE_ADDR'] == "89.95.25.37") die();
Would block specific IP. How can I block entire IP blocks?
Thank you very much.
Try strpos()
if(strpos($_SERVER['REMOTE_ADDR'], "89.95") === 0)
{
die();
}
If you notice, the ===
operator makes sure that the 89.95
is at the begining of the IP address. This means that you can sepcify as much of the IP address as you want, and it will block no matter what numbers come after it.
For instance, all of these will be blocked:
89.95 -> 89.95.12.34
, 89.95.1234.1
, 89.95.1.1
89.95.6 -> 89.95.65.34
, 89.95.61.1
, 89.95.6987
(some of those aren't valid IP addresses though)
这篇关于在我的网站上用PHP阻止特定的IP块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!