PHP implementation code to prevent Ddos, DNS, and cluster attacks

WBOY
Release: 2016-07-25 08:58:34
Original
996 people have browsed it
  1. /**
  2. * Prevent ddos, dns, cluster and other attacks
  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."rn",$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."rn",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."1rn";
  60. elseif($numtem<$allowNum)
  61. $str.=$iptem.$uritem.$timetem.($numtem+1)."rn";
  62. else {
  63. if(!file_exists($fileforbid)) {
  64. $addforbidarr=array($ip."rn",time()."rn",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."rn",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."1rn";
  75. file_put_contents($file,$str);
  76. ?>
复制代码


Related labels:
dns
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!