PHP IP-адрес в текстовом файле со счетчиком и пинками 403 Запрещено
Это правильный код... наслаждайтесь
Результат:
66.249.69.212 | 6 | crawl-66-249-69-212.googlebot.com
157.55.39.251 | 8 | msnbot-157-55-39-251.search.msn.com
76.249.69.210 | 3 | crawl-66-249-69-210.alex.com
<?php
$ip_file = 'g:/logfiles/blocked_403_ip.txt';
$ip_short = $_SERVER['REMOTE_ADDR'];
$ip_long = gethostbyaddr($ip_short);
$rows = file($ip_file, FILE_IGNORE_NEW_LINES);
$list = array();
if (!empty($rows)) {
foreach ($rows as $r => $row) {
$list[$r] = str_getcsv($row, '|');
}
}
$isNewIp = true;
foreach ($list as $k => $l) {
if ($l[0] == $ip_short) {
$list[$k][1]++;
$isNewIp = false;
break;
}
}
if ($isNewIp) {
$list[] = [
$ip_short,
1,
$ip_long
];
}
$fileContent = '';
foreach($list as $l) {
$fileContent .= implode('|', $l) . "\r\n";
}
file_put_contents($ip_file, $fileContent);
header("HTTP/1.0 403 Forbidden");
ignore_user_abort(true);
ob_start();
$size = ob_get_length();
header("Content-Encoding: none");
header("Content-Length: $size");
header("Content-Type: text/plain");
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
if (session_id()) session_write_close();
ob_end_clean();
exit();
?>