PHP to prevent Ddos,dns, cluster attack implementation code

Source: Internet
Author: User
  1. /**
  2. * Protection against DDoS, DNS, cluster attacks
  3. * Edit bbs.it-home.org
  4. */
  5. Query prohibit 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 is forbided by some reason and IF you had any question Pls Emill to shop@jbxue.com! ");
  13. Add Forbidden 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. Anti-Refresh
  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;//anti-refresh Time
  42. $allowNum =10;//anti-refresh times
  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 is 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. ?>
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.