当前位置:Gxlcms > PHP教程 > php中关于ddos安全处理的示例代码分享

php中关于ddos安全处理的示例代码分享

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

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

以上就是php中关于ddos安全处理的示例代码分享的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行