rendered paste bodyfunction check_flood() { $ip = ip2long($addr = $_SERVER['HTTP_X_FORWARDED_FOR']); if (!$ip or ($ip == -1) or (($ip >> 24) == 10) or (($ip >> 20) == -0x53f) or (($ip >> 16) == -0x3f58)) $ip = ip2long($addr = $_SERVER['REMOTE_ADDR']); #debug("Checking for flood. addr=$addr ip=$ip"); mysql_query("delete from blocked where last<now() - interval count+3600 second"); mysql_query("delete from requests where time<now() - interval 5 minute"); mysql_query(mkquery("update blocked set count=count+1 where ip=%d", $ip)); $cnt = mysql_affected_rows(); if ($cnt > 0) { header("HTTP/1.0 403 Flood detected."); exit; } mysql_query(mkquery("insert into requests (ip) values(%d)", $ip)); $rqcnt = mysql_fetch_row(mysql_query(mkquery('select count(*),count(if(time>now()-interval 1 minute,1,NULL)),min(time) from requests where ip=%d', $ip))); #debug("$addr = $ip had $rqcnt[0]/$rqcnt[1] requests in the last 60 seconds/since $rqcnt[2], blocked cnt $cnt."); if ($rqcnt[0] > 60 || $rqcnt[1] > 25) { dolog("Flood detected ($rqcnt[0] from $addr), blocking."); mysql_query(mkquery('insert into blocked (first,ip,count) values("%s",%d,%d)', $rqcnt[2],$ip,$rqcnt[0])); header("HTTP/1.0 403 Flood detected."); exit; }}