Но протестить его не смогу, поэтому прошу посмотрите, насколкьо он рабочий?
Создаём таблицу в БД:
CODE:
CREATE TABLE `all_visits` (
`ip` varchar(15) DEFAULT NULL,
`date` int(11) DEFAULT NULL,
INDEX (`date` Desc)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
`ip` varchar(15) DEFAULT NULL,
`date` int(11) DEFAULT NULL,
INDEX (`date` Desc)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Дальше идёт сам скрипт, который всё делает.
CODE:
<?php
if (!empty($_SERVER['HTTP_CLIENT_IP']))
$ip=$_SERVER['HTTP_CLIENT_IP'];
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
else $ip=$_SERVER['REMOTE_ADDR'];
$bot=$_SERVER['HTTP_USER_AGENT'];
if (strstr($_SERVER['HTTP_USER_AGENT'], 'Yandex')) {$bot='Allowed_bot';}
elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'Google')) {$bot='Allowed_bot';}
elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'Yahoo')) {$bot='Allowed_bot';}
elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'Mail.Ru')) {$bot='Allowed_bot';}
elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'Bing')) {$bot='Allowed_bot';}
if ($bot!='Allowed_bot') {
$link=mysql_connect("IP-адрес-MYSQL","Login_mysql","Password_mysql") or die("Could not connect: ".mysql_error());
mysql_select_db("ИМЯ_БД", $link);
$result=mysql_query("INSERT INTO all_visits (ip,date)
VALUES ('".$ip."','".time(true)."')");
$result=mysql_query("SELECT count(1) FROM all_visits
WHERE (ip='".$ip."' and date>'".(time(true)-5)."')", $link);
$count_visit=mysql_fetch_array($result);
if ($count_visit[0]>30) {
$file_htaccess=".htaccess";
$start_line=0;
$lines=file($file_htaccess);
for ($n=0; $n<=count($lines)-1; $n++) {
$lines_htaccess[]=$lines[$n];
if ($lines[$n] == "<Limit GET POST> \r\n") {
$lines_htaccess[]=$lines[$n+1]; //order allow,deny
$start_line=$n+2;
break;
}
}
if ($start_line!=0) {
$lines_htaccess[]=" deny from ".$ip."\r\n";
for ($n=$start_line; $n<=count($lines); $n++) $lines_htaccess[]=$lines[$n];
} else {
$lines_htaccess = $lines;
$lines_htaccess[]="<Limit GET POST> \r\n";
$lines_htaccess[]=" order allow,deny \r\n";
$lines_htaccess[]=" deny from ".$ip."\r\n";
$lines_htaccess[]=" allow from all \r\n";
$lines_htaccess[]="</Limit> \r\n\r\n";
}
file_put_contents($file_htaccess, $lines_htaccess);
}
}
?>
if (!empty($_SERVER['HTTP_CLIENT_IP']))
$ip=$_SERVER['HTTP_CLIENT_IP'];
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
else $ip=$_SERVER['REMOTE_ADDR'];
$bot=$_SERVER['HTTP_USER_AGENT'];
if (strstr($_SERVER['HTTP_USER_AGENT'], 'Yandex')) {$bot='Allowed_bot';}
elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'Google')) {$bot='Allowed_bot';}
elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'Yahoo')) {$bot='Allowed_bot';}
elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'Mail.Ru')) {$bot='Allowed_bot';}
elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'Bing')) {$bot='Allowed_bot';}
if ($bot!='Allowed_bot') {
$link=mysql_connect("IP-адрес-MYSQL","Login_mysql","Password_mysql") or die("Could not connect: ".mysql_error());
mysql_select_db("ИМЯ_БД", $link);
$result=mysql_query("INSERT INTO all_visits (ip,date)
VALUES ('".$ip."','".time(true)."')");
$result=mysql_query("SELECT count(1) FROM all_visits
WHERE (ip='".$ip."' and date>'".(time(true)-5)."')", $link);
$count_visit=mysql_fetch_array($result);
if ($count_visit[0]>30) {
$file_htaccess=".htaccess";
$start_line=0;
$lines=file($file_htaccess);
for ($n=0; $n<=count($lines)-1; $n++) {
$lines_htaccess[]=$lines[$n];
if ($lines[$n] == "<Limit GET POST> \r\n") {
$lines_htaccess[]=$lines[$n+1]; //order allow,deny
$start_line=$n+2;
break;
}
}
if ($start_line!=0) {
$lines_htaccess[]=" deny from ".$ip."\r\n";
for ($n=$start_line; $n<=count($lines); $n++) $lines_htaccess[]=$lines[$n];
} else {
$lines_htaccess = $lines;
$lines_htaccess[]="<Limit GET POST> \r\n";
$lines_htaccess[]=" order allow,deny \r\n";
$lines_htaccess[]=" deny from ".$ip."\r\n";
$lines_htaccess[]=" allow from all \r\n";
$lines_htaccess[]="</Limit> \r\n\r\n";
}
file_put_contents($file_htaccess, $lines_htaccess);
}
}
?>
Будет работать это?
Может быть есть проще способ?
Суть, как я понял, в том, что создаётся чёрный список IP, кто обращается чаще 30 раз за 5 секунд.