douqin7086 2014-07-17 15:45
浏览 8
已采纳

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

Suppose I wanted to ban a specific IP address from accessing the whole domain name (+ all included subdomains). At first I began with the following snippet to add the following lines to the .htaccess file:

$info = 'Order Deny,Allow
Deny from' . IPtoBlock();

if (getIP()){
    $htaccess = fopen('.htaccess', 'r+');
    fwrite($htaccess, $info);
    fclose($htaccess);
}

But is it more relevant to redirect the user to something else? After all, he is still capable of making a request towards the server despite the immediate redirect.

$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/");
}

or simply kill the page?

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

What is the best approach towards this issue?

  • 写回答

3条回答 默认 最新

  • doujun7161 2014-07-17 15:49
    关注

    In jour .htaccess :

    Order Allow,Deny
    Allow from all
    Deny from 123.123.123.123
    

    User with ip 123.123.123.123 will have a 403.

    And if you want to redirect to a specific page, add :

    ErrorDocument 403 /forbidden.php
    

    Edit : For ban Ip from text file in htaccess, take a look here : Ban IPs from text file using htaccess

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应