The function of automatically blocking the other party's IP when being attacked by CC or DDOS

WBOY
Release: 2016-07-25 08:45:55
Original
1110 people have browsed it
This function is used to count how many times each visitor has visited in a short period of time. If the number of visits exceeds the limit, it returns TRUE. After that, you can use PHP to call linux's iptables to perform the blocking operation. I have used several I have actually tested DDOS tools and the results are very good.
By the way, I use files in the code to record the visitor IP and time. It is best not to use a database (and don’t be clever and save it in the session) , in addition, it is best to put this file on the SSD hard drive. I won’t go into details for the reason. I think everyone knows it.
    /**
  1. Prevent malicious reorganization on the client side
  2. Usage:
  3. $isf5=Fun::isf5();
  4. Return:
  5. Return bool[true: the other party is maliciously reorganizing; false: normal access]
  6. /**/
  7. public static function isf5(){
  8. $_f=Run.'_isf5';
  9. if(!file_exists($_f)){
  10. file_put_contents($_f,serialize(array) ()),LOCK_EX);
  11. chmod($_f,0777);
  12. }
  13. $arr=unserialize(file_get_contents($_f));
  14. $arr=(!is_array($arr)) ? array() : $arr ;
  15. //Clear out users who visited 10 seconds ago
  16. foreach($arr as $k=>$v){
  17. if($_ENV['now']-$v['t'] >= 10 ){
  18. unset($arr[$k]);
  19. }
  20. }
  21. $ip='_'.(self::cur('ip'));
  22. if(!isset($arr[$ip ])){
  23. $arr[$ip]['n']=1; //The number of connections within 1s
  24. $arr[$ip]['t']=$_ENV['now']; // Time of the first access
  25. file_put_contents($_f,serialize($arr),LOCK_EX);
  26. chmod($_f,0777);
  27. unset($_f,$arr,$ip);
  28. return FALSE;
  29. }else {
  30. if(!isset($arr[$ip]['t']) or !is_numeric($arr[$ip]['t'])){
  31. unset($arr[$ip]);
  32. file_put_contents ($_f,serialize($arr),LOCK_EX);
  33. chmod($_f,0777);
  34. unset($_f,$arr,$ip);
  35. return FALSE;
  36. }
  37. if(($_ENV['now ']-$arr[$ip]['t']) <= 1){ //If the time since the last access is not more than 1s, only the number of times will be accumulated
  38. $arr[$ip]['n'] +=1;
  39. if($arr[$ip]['n']>=5){
  40. unset($_f,$arr,$ip);
  41. return TRUE;
  42. }else{
  43. file_put_contents($_f ,serialize($arr),LOCK_EX);
  44. chmod($_f,0777);
  45. unset($_f,$arr,$ip);
  46. return FALSE;
  47. }
  48. }else{ //If the distance from the last visit If the time has exceeded 1s, count again
  49. $arr[$ip]['n']=1;
  50. $arr[$ip]['t']=$_ENV['now'];
  51. file_put_contents($_f, serialize($arr),LOCK_EX);
  52. chmod($_f,0777);
  53. unset($_f,$arr,$ip);
  54. return FALSE;
  55. }
  56. }
  57. unset($_f,$arr,$ip );
  58. return FALSE;
  59. }
Copy code
DDOS, function

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!