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