电脑软硬件应用网
当前位置: 电脑软硬件应用网 > 设计学院 > 网络编程 > PHP教程 > 正文
超级有用网站防IP攻击代码详解
超级有用网站防IP攻击代码详解
2010-11-22 12:17:28  文/佚名   出处:博客园   

在博客园看到这篇最新开发的网站防IP攻击代码,觉得非常不错,作者是使用PHP语言来开发,所以推荐给大家看下

1 <?php
2  //查询禁止IP
3 $ip =$_SERVER['REMOTE_ADDR'];
4 $fileht=".htaccess2";
5 if(!file_exists($fileht))file_put_contents($fileht,"");
6 $filehtarr=@file($fileht);
7 if(in_array($ip."\r\n",$filehtarr))die("Warning:"."<br>"."Your IP address are forbided by some reason, IF you have any question Pls emill to shop@mydalle.com!");
8
9
10 //加入禁止IP
11 $time=time();
12 $fileforbid="log/forbidchk.dat";
13
14 if(file_exists($fileforbid))
15 {   if($time-filemtime($fileforbid)>60)unlink($fileforbid);
16 else{
17     $fileforbidarr=@file($fileforbid);
18     if($ip==substr($fileforbidarr[0],0,strlen($ip)))
19             {
20             if($time-substr($fileforbidarr[1],0,strlen($time))>600)unlink($fileforbid);
21             elseif($fileforbidarr[2]>600){file_put_contents($fileht,$ip."\r\n",FILE_APPEND);unlink($fileforbid);}
22             else{$fileforbidarr[2]  ;file_put_contents($fileforbid,$fileforbidarr);}   
23            }
24     }
25 }
26
27 //防刷新
28 $str="";
29 $file="log/ipdate.dat";
30 if(!file_exists("log")&&!is_dir("log"))mkdir("log",0777);
31 if(!file_exists($file))file_put_contents($file,"");
32 $allowTime = 120;//防刷新时间
33 $allowNum=10;//防刷新次数
34 $uri=$_SERVER['REQUEST_URI'];
35 $checkip=md5($ip);
36 $checkuri=md5($uri);
37 $yesno=true;
38 $ipdate=@file($file);
39 foreach($ipdate as $k=>$v)
40 {      $iptem=substr($v,0,32);
41       $uritem=substr($v,32,32);
42       $timetem=substr($v,64,10);
43       $numtem=substr($v,74);   
44     if($time-$timetem<$allowTime){
45       if($iptem!=$checkip)$str.=$v;
46 else{
47     $yesno=false;     
48        if($uritem!=$checkuri)$str.=$iptem.$checkuri.$time."1\r\n";
49        elseif($numtem<$allowNum)$str.=$iptem.$uritem.$timetem.($numtem 1)."\r\n";
50         else
51         {  
52             if(!file_exists($fileforbid)){$addforbidarr=array($ip."\r\n",time()."\r\n",1);file_put_contents($fileforbid,$addforbidarr);}
53             file_put_contents("log/forbided_ip.log",$ip."--".date("Y-m-d H:i:s",time())."--".$uri."\r\n",FILE_APPEND);
54             $timepass=$timetem $allowTime-$time;
55             die("Warning:"."<br>"."Sorry,you are forbided by refreshing frequently too much, Pls wait for ".$timepass." seconds to continue!");
56     }   
57 }
58 }
59 }
60 if($yesno) $str.=$checkip.$checkuri.$time."1\r\n";
61 file_put_contents($file,$str);
62 ?>

把程序include进要进行的php文件头部。
程序首先判断IP是否在禁止列表,如果在则退出;
否则,如果IP在监控列表,10分钟内点击超过600次则加入禁止列表。
如果没有超过时间和次数则次数加1,同时,监控IP是否对同一页面频繁操作。
由于是机器刷频,所以程序不用session判断。
还有就是更新的时候,不能用独占处理文件。
使用伪静态存储IP也会出错。
这个程序还有变化和改进的余地的,大家也可以参考QQ农场里的降级原理,基本上农场也是按照这个原理开发的吧。

  • 上一篇文章:

  • 下一篇文章:
  • 最新热点 最新推荐 相关文章
    解决PHP设置使用date或mktime函数问…
    分享PHP实现Web系统单点登录的方法
    PHP实用对付IP攻击升级程序改进
    Cannot start session without erro…
    PHP开发应用程序:生成随机字符串的方…
    PHP开发技巧:PHP网站发开实用技巧集…
    详解如何正确配置PHP开发环境
    PHP里实现汉字转区位码的示例代码
    PHP实现列出目录内容与删除目录的内…
    PHP解析JSON与xml数据的代码
    关于45IT | About 45IT | 联系方式 | 版权声明 | 网站导航 |

    Copyright © 2003-2011 45IT. All Rights Reserved 浙ICP备09049068号