当前位置:Gxlcms > PHP教程 > php防止ddos,dns,集群攻击的实现代码

php防止ddos,dns,集群攻击的实现代码

时间:2021-07-01 10:21:17 帮助过:14人阅读

  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. ?>

人气教程排行