限制IP地址的最好方法是什么?


What's the best way to restrict IP address?

假设我想禁止一个特定的IP地址访问整个域名(+所有包含的子域名)。首先,我从下面的代码片段开始,将以下行添加到.htaccess文件中:

$info = 'Order Deny,Allow
Deny from' . IPtoBlock();
if (getIP()){
    $htaccess = fopen('.htaccess', 'r+');
    fwrite($htaccess, $info);
    fclose($htaccess);
}

但是,将用户重定向到其他内容是否更相关?毕竟,尽管进行了立即重定向,他仍然能够向服务器发出请求。

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

还是直接删除页面?

$deny = array('192.168.1.0', '192.168.1.1', '192.168.1.2');
if (in_array ($_SERVER['REMOTE_ADDR'], $deny)) {
   die('Access restricted');
}

解决这个问题的最好方法是什么?

In jour .htaccess:

Order Allow,Deny
Allow from all
Deny from 123.123.123.123

ip为123.123.123.123的用户会有一个403。

如果你想重定向到一个特定的页面,添加:

ErrorDocument 403 /forbidden.php

编辑:禁止Ip从文本文件在htaccess,看看这里:禁止Ip从文本文件使用htaccess

在您的.htacces文件:

Order Deny,Allow
Deny from all
Allow from xx.xx            // This will be your local IP address
Allow from yy.yy.yy.yy      // Your server IP address

除了这些情况,没有人可以访问你的网站。

最好在它到达您的web服务器之前阻止这种尝试。这意味着在iptables(防火墙;或者如果你不能这样做,并且正在使用负载均衡器,那么就在那里阻止它)。