【问题标题】:What's the best way to restrict IP address?限制IP地址的最佳方法是什么?
【发布时间】:2014-07-18 00:00:13
【问题描述】:

假设我想禁止特定 IP 地址访问整个域名(+ 所有包含的子域)。起初我从以下 sn -p 开始,将以下行添加到 .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');
}

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

【问题讨论】:

  • 不要在 php 级别这样做。至少在网络服务器级别执行此操作。或者更好的是,在防火墙/路由器级别。如果你不想让别人接触你的东西,甚至不要让他们进入前门。
  • 我会坚持使用 .htaccess。然后你可以设计一个自定义的 403 页面,使用ErrorDocument 403 forbidden.html。但是,如果您只想向他们显示错误,如果他们危险到足以禁止整个网站,也许它们危险到足以在整个网络级别(udp、tcp 等)禁止。
  • 这是有道理的,但我并不总是控制服务器的人,我想确保某些页面不公开。
  • 某些页面,或the whole domain name (+ all included subdomains)?如果您要禁止的 IP 是基于列表的,并且您只是从特定页面禁止它们,那么您可能想尝试您的 PHP 技术。

标签: php .htaccess


【解决方案1】:

在路上.htaccess

Order Allow,Deny
Allow from all
Deny from 123.123.123.123

IP 为 123.123.123.123 的用户将获得 403。

如果您想重定向到特定页面,请添加:

ErrorDocument 403 /forbidden.php

编辑:要禁止在 htaccess 中的文本文件中使用 Ip,请看这里:Ban IPs from text file using htaccess

【讨论】:

【解决方案2】:

在您的.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

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

【讨论】:

    【解决方案3】:

    最好在此尝试到达您的网络服务器之前阻止它。这意味着在 iptables(防火墙;或者如果您不能这样做并且正在使用负载平衡器,则将其阻止在那里)这样的级别进行阻塞。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-30
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      相关资源
      最近更新 更多