如何使用PHP阻止多个IP地址


How Can I Block Multiple IP Addresses with PHP

我想通过IP屏蔽一些BAD用户。

所以我需要一个简单的PHP代码,可以阻止多个IP地址,并将它们重定向到其他网站。

现在我使用这个代码

<?php
$deny = array("111.111.111", "222.222.222", "333.333.333");
if (preg_match($_SERVER['REMOTE_ADDR'], $deny)) {
header("location: http://www.google.com/");
exit();
} ?>

不使用preg_match,而是使用in_array函数,因为preg_match接受第二个参数(要匹配的表达式)作为字符串。

使用以下代码:

if (in_array($_SERVER['REMOTE_ADDR'] , $deny)) {
    header("location: http://www.google.com/");
    exit();
}