MakStashkevich

ЗАЩИТА ОТ DDOS

Nov 8th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. /////////////////// ЗАЩИТА ОТ DDOS АТАК ///////////////////
  2.  
  3. // $link -- соединение с БД, $ip_text -- IP-адрес
  4. define('ALERT_TIME',300);
  5. define('ALERT_COUNT',150);  
  6.  
  7. function check_dos($ip_text, $link)  {
  8.   $ip=ip2long($ip_text);
  9.   $res=mysql_query($link,'SELECT lasttime, count, status FROM ip_check WHERE ip="'.$ip.'"');
  10.   if (mysql_num_rows($res)==0) { // нет такого IP
  11.   $count=1;
  12.   mysql_query($link,'INSERT INTO ip_check (lasttime,count,ip,status) VALUES ('.time().',1,"'.$ip.'",0)');
  13.   }
  14. else {
  15.   $data=mysql_fetch_row($res);
  16.   $status=$data[2];
  17.   if ($status!=2 && ($data[0]<time()-ALERT_TIME || $data[1]<ALERT_COUNT)))   $count=1;
  18.   elseif ($status!=2) { $count=$data[1]+1; if ($count>=ALERT_COUNT) $status=1; }
  19.   if ($status!=2) {
  20.     mysql_query($link,'UPDATE ip_check SET count='.$count,', status='.$status.', lasttime='.time().' WHERE ip="'.$ip.'"';
  21.   }
  22.   if ($status) {
  23.     header($_SERVER['HTTP_PROTOCOL'].' 503 Temporary Unavailable');
  24.     trigger_error('Превышено число допустимых запросов!',E_USER_ERROR);
  25.   }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment