Automatically block the function of the other IP when a cc or DDoS attack occurs

Source: Internet
Author: User
This function is used to count how many times each visitor has visited in a short period of time, and returns true if the number of times limit is exceeded, after which you can use PHP to call Linux iptables for blocking operations.
I've used several DDoS-like tools to actually test it, and it works really well.
By the way, I use files in the code to record the visitor's IP and time, it is best not using the database (nor smart to save the session), it is also best to put this file into the SSD hard disk, the reason why I don't say, I guess everyone knows
  1. /**
  2. Prevent customer-side evil reorganization
  3. Usage:
  4. $ISF 5=fun::isf5 ();
  5. Return:
  6. Return to Bool[true: The opposite party is in a wicked reorganization; false: Normal interview]
  7. /**/
  8. public static function Isf5 () {
  9. $_f=run. ' _isf5 ';
  10. if (!file_exists ($_f)) {
  11. File_put_contents ($_f,serialize (Array ()), LOCK_EX);
  12. chmod ($_f,0777);
  13. }
  14. $arr =unserialize (file_get_contents ($_f));
  15. $arr = (!is_array ($arr))? Array (): $arr;
  16. Clean up the user who was interviewed 10 seconds ago
  17. foreach ($arr as $k = = $v) {
  18. if ($_env[' now ']-$v [' t '] >= 10) {
  19. Unset ($arr [$k]);
  20. }
  21. }
  22. $ip = ' _ '. (Self::cur (' IP '));
  23. if (!isset ($arr [$ip])) {
  24. $arr [$ip] [' N ']=1;//1s
  25. $arr [$ip] [t ']=$_env[' now '];//the 1th time
  26. File_put_contents ($_f,serialize ($arr), LOCK_EX);
  27. chmod ($_f,0777);
  28. Unset ($_f, $arr, $IP);
  29. return FALSE;
  30. }else{
  31. if (!isset ($arr [$ip] [' t ']) or!is_numeric ($arr [$ip] [' t '])) {
  32. Unset ($arr [$ip]);
  33. File_put_contents ($_f,serialize ($arr), LOCK_EX);
  34. chmod ($_f,0777);
  35. Unset ($_f, $arr, $IP);
  36. return FALSE;
  37. }
  38. if ($_env[' now ')-$arr [$ip [' t ']) <= 1) {//If the distance from the last interview is not over 1s, it only accumulates the number of times
  39. $arr [$ip] [' N ']+=1;
  40. if ($arr [$ip] [' n ']>=5) {
  41. Unset ($_f, $arr, $IP);
  42. return TRUE;
  43. }else{
  44. File_put_contents ($_f,serialize ($arr), LOCK_EX);
  45. chmod ($_f,0777);
  46. Unset ($_f, $arr, $IP);
  47. return FALSE;
  48. }
  49. }else{//if the distance from the last visit has been over 1s, then re-calculate
  50. $arr [$ip] [' N ']=1;
  51. $arr [$ip] [' t ']=$_env[' now '];
  52. File_put_contents ($_f,serialize ($arr), LOCK_EX);
  53. chmod ($_f,0777);
  54. Unset ($_f, $arr, $IP);
  55. return FALSE;
  56. }
  57. }
  58. Unset ($_f, $arr, $IP);
  59. return FALSE;
  60. }
Copy Code
DDOS, function
  • 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.