The function of the IP address of the other party is automatically blocked when CC or DDOS attacks occur.

Source: Internet
Author: User
The function of the IP address of the other party is automatically blocked when CC or DDOS attacks occur.
This function is used to count the total number of visits per visitor in a short period of time. if the limit is exceeded, TRUE is returned, then you can use PHP to call linux iptables for blocking.
I have used several DDOS tools for actual testing, and the results are very good.
By the way, I use a file in the code to record the ip address and time of the visitor. it is best not to use a database (or store it in the session smartly ), in addition, it is best to put this file on an SSD hard disk. I won't say the reason. we all know it.

  1. /**
  2. Prevents the customer receiver from being intentionally reorganized
  3. Usage:
  4. $ Isf5 = Fun: isf5 ();
  5. Return value:
  6. Return bool [true: the operator indicates the reorganization in intent; false: normal response]
  7. /**/
  8. Public static function isf5 (){
  9. $ _ F = Run. '_ isf ';
  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. // Clear the token used for the last 10 seconds.
  17. Foreach ($ arr as $ k =>$ v ){
  18. If ($ _ ENV ['right']-$ v ['t']> = 10 ){
  19. Unset ($ arr [$ k]);
  20. }
  21. }
  22. $ Ip = '_'. (self: cur ('IP '));
  23. If (! Isset ($ arr [$ ip]) {
  24. $ Arr [$ ip] ['n'] = 1; // The number of batch requests in 1 S
  25. $ Arr [$ ip] ['t'] = $ _ ENV ['right']; // the time of the first 1st million Reply requests
  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 ['right']-$ arr [$ ip] ['t']) <= 1) {// if there is not more than 1 s since the time when the last response was asked by the queue, only the number of requests is accumulated.
  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 time from the last time the queue was asked has exceeded 1 s, the data will be recalculated.
  50. $ Arr [$ ip] ['n'] = 1;
  51. $ Arr [$ ip] ['t'] = $ _ ENV ['right'];
  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. }

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.