我正在使用以下代码段来重定向IP地址数组.我想知道如何在我的不允许数组中添加整个范围/块IP地址...
<?php // Let's redirect certain IP addresses to a "Page Not Found"
$disallowed = array("76.105.99.106");
$ip = $_SERVER['REMOTE_ADDR'];
if(in_array($ip, $disallowed)) {
header("Location: http://google.com");
exit;
}
?>
Run Code Online (Sandbox Code Playgroud)
我尝试使用"76.105.99.*","76.105.99","76.105.99.0-76.105.99.255"没有任何运气.
出于其他原因,我需要使用PHP而不是mod_rewrite和.htaccess.
Pau*_*xon 11
以下是如何检查特定网络/掩码组合的示例:
$network=ip2long("76.105.99.0");
$mask=ip2long("255.255.255.0");
$remote=ip2long($_SERVER['REMOTE_ADDR']);
if (($remote & $mask)==$network)
{
header("Location: http://example.com");
exit;
}
Run Code Online (Sandbox Code Playgroud)
这比使用基于字符串的匹配更好,因为您可以测试在八位字节内对齐的其他掩码,例如/ 20块IP
尝试一下这个substr
功能:
$ip = '76.105.99.';
if (substr($_SERVER['REMOTE_ADDR'], 0, strlen($ip)) === $ip) {
// deny access
}
Run Code Online (Sandbox Code Playgroud)