php防止ddos,dns,叢集攻擊的實現代碼

來源:互聯網
上載者:User
  1. /**
  2. * 防止ddos、dns、叢集等攻擊
  3. * edit bbs.it-home.org
  4. */
  5. //查詢禁止IP
  6. $ip =$_SERVER['REMOTE_ADDR'];
  7. $fileht=".htaccess2";
  8. if(!file_exists($fileht))
  9. file_put_contents($fileht,"");
  10. $filehtarr=@file($fileht);
  11. if(in_array($ip."\r\n",$filehtarr))
  12. die("Warning:"."
    "."Your IP address are forbided by some reason, IF you have any question Pls emill to shop@jbxue.com!");
  13. //加入禁止IP
  14. $time=time();
  15. $fileforbid="log/forbidchk.dat";
  16. if(file_exists($fileforbid)) {
  17. if($time-filemtime($fileforbid)>60)
  18. unlink($fileforbid);
  19. else {
  20. $fileforbidarr=@file($fileforbid);
  21. if($ip==substr($fileforbidarr[0],0,strlen($ip))) {
  22. if($time-substr($fileforbidarr[1],0,strlen($time))>600)
  23. unlink($fileforbid);
  24. elseif($fileforbidarr[2]>600) {
  25. file_put_contents($fileht,$ip."\r\n",FILE_APPEND);
  26. unlink($fileforbid);
  27. } else {
  28. $fileforbidarr[2]++;
  29. file_put_contents($fileforbid,$fileforbidarr);
  30. }
  31. }
  32. }
  33. }
  34. //防重新整理
  35. $str="";
  36. $file="log/ipdate.dat";
  37. if(!file_exists("log")&&!is_dir("log"))
  38. mkdir("log",0777);
  39. if(!file_exists($file))
  40. file_put_contents($file,"");
  41. $allowTime = 120;//防重新整理時間
  42. $allowNum=10;//防重新整理次數
  43. $uri=$_SERVER['REQUEST_URI'];
  44. $checkip=md5($ip);
  45. $checkuri=md5($uri);
  46. $yesno=true;
  47. $ipdate=@file($file);
  48. foreach($ipdate as $k=>$v) {
  49. $iptem=substr($v,0,32);
  50. $uritem=substr($v,32,32);
  51. $timetem=substr($v,64,10);
  52. $numtem=substr($v,74);
  53. if($time-$timetem<$allowTime) {
  54. if($iptem!=$checkip)
  55. $str.=$v;
  56. else {
  57. $yesno=false;
  58. if($uritem!=$checkuri)
  59. $str.=$iptem.$checkuri.$time."1\r\n";
  60. elseif($numtem<$allowNum)
  61. $str.=$iptem.$uritem.$timetem.($numtem+1)."\r\n";
  62. else {
  63. if(!file_exists($fileforbid)) {
  64. $addforbidarr=array($ip."\r\n",time()."\r\n",1);
  65. file_put_contents($fileforbid,$addforbidarr);
  66. }
  67. file_put_contents("log/forbided_ip.log",$ip."--".date("Y-m-d H:i:s",time())."--".$uri."\r\n",FILE_APPEND);
  68. $timepass=$timetem+$allowTime-$time;
  69. die("Warning:"."
    "."Sorry,you are forbided by refreshing frequently too much, Pls wait for ".$timepass." seconds to continue!");
  70. }
  71. }
  72. }
  73. }
  74. if($yesno) $str.=$checkip.$checkuri.$time."1\r\n";
  75. file_put_contents($file,$str);
  76. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.