php--防止DDos攻擊代碼

來源:互聯網
上載者:User

標籤:amp   end   ++   fresh   put   continue   for   file   mil   

 1 <?php 2 //查詢禁止IP 3 $ip =$_SERVER[‘REMOTE_ADDR‘]; 4 $fileht=".htaccess2"; 5 if(!file_exists($fileht))file_put_contents($fileht,""); 6 $filehtarr[email protected]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 [email protected]!"); 8  9 //加入禁止IP10 $time=time();11 $fileforbid="log/forbidchk.dat";12 if(file_exists($fileforbid))13 { if($time-filemtime($fileforbid)>60)unlink($fileforbid);14 else{15     $fileforbidarr[email protected]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[email protected]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             else48             {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 ?>

 

php--防止DDos攻擊代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.