Ok, here is my IP blocking system.
if (in_array($page, explode(', ', $settings['block_pages']))) {
$sql = "SELECT `blc_id`,`blc_status` FROM `blocks` WHERE `blc_ip` = ".qs($_SERVER['REMOTE_ADDR'])." AND `blc_page` = ".qs($page)." AND `blc_time` > UNIX_TIMESTAMP()";
$result = mysql_query($sql) OR exit(mysql_error());
$ipblock = mysql_fetch_assoc($result);
if ($ipblock != NULL) {
if ($ipblock['blc_status'] >= $settings['block_count']) {
header('Location: http://site.com/?error');
exit;
} else {
$sql = "UPDATE `blocks` SET `blc_status` = `blc_status`+1, `blc_time` = UNIX_TIMESTAMP()+".intval($settings['block_time'])." WHERE `blc_id` = ".intval($ipblock['blc_id']);
$result = mysql_query($sql) OR exit(mysql_error());
}
} else {
$sql = "INSERT INTO `blocks` SET `blc_ip` = ".qs($_SERVER['REMOTE_ADDR']).", `blc_page` = ".qs($page).", `blc_time` = UNIX_TIMESTAMP()+".intval($settings['block_time']);
$result = mysql_query($sql) OR exit(mysql_error());
}
}
P.S. function qs() works with mysql_real_escape_string().
$settings['block_pages'] - pages, in which blocking is enabled, for example login and admin/login.
$settings['block_count'] = amount of times after which the access to page is blocked.
$settings['block_time'] = time period (in seconds) in which block is activated.
I allow 5 attempts in 300 seconds and then block visitor for the same time. Is it ok or better to use different settings?