Правило php preg для обнаружения ботов?

  • Автор темы yarthefish
  • 34
  • Обновлено
  • 16, May 2024
  • #1
Я нашел это в своей статистике AW: Неизвестный робот (обозначается словом «бот», за которым следует пробел или один из следующих символов _+:,.;/\-) 636 127+83 17,95 ГБ

Это то, что потребляет максимальную пропускную способность моего сайта.

В 4 раза больше, чем потребляет бот Google.

Я не знаю, что это за боты и какой объем трафика они отправляют, но общее количество обращений ботов на мой сайт в этом месяце превысило 1 миллион, что привело к появлению сообщения об ошибке «превышена пропускная способность».

Есть ли способ заблокировать все это с помощью php preg?

Но я не хочу блокировать ботов Google, Yahoo и Bing.

Может кто-нибудь дать мне правило preg_match в PHP для обнаружения всех ботов?

Я хочу использовать PHP, чтобы регистрировать их обращения в файл журнала перед отправкой кода 403.

Спасибо

yarthefish


Рег
01 Jan, 2011

Тем
1

Постов
1

Баллов
11
  • 18, May 2024
  • #2
Я не знаю, видели ли вы это когда-нибудь или использовали. Там вы можете назначить хороших ботов и заблокировать всех остальных ботов. Его следует разместить прямо вверху индексной страницы.
 

<?php

// ---------------------------------------------------------------------------------------------------------------

// Banned IP Addresses and Bots - Redirects banned visitors who make it past the .htaccess and or robots.txt files to an URL.

// The $banned_ip_addresses array can contain both full and partial IP addresses, i.e. Full = 123.456.789.101, Partial = 123.456.789. or 123.456. or 123.

// Use partial IP addresses to include all IP addresses that begin with a partial IP addresses. The partial IP addresses must end with a period.

// The $banned_bots, $banned_unknown_bots, and $good_bots arrays should contain keyword strings found within the User Agent string.

// The $banned_unknown_bots array is used to identify unknown robots (identified by 'bot' followed by a space or one of the following characters _+:,.;/\-).

// The $good_bots array contains keyword strings used as exemptions when checking for $banned_unknown_bots. If you do not want to utilize the $good_bots array such as

// $good_bots = array(), then you must remove the the keywords strings 'bot.','bot/','bot-' from the $banned_unknown_bots array or else the good bots will also be banned.

$banned_ip_addresses = array('');

$banned_bots = array('.ru','AhrefsBot','crawl','crawler','DotBot','linkdex','majestic','meanpath','PageAnalyzer','robot','rogerbot','semalt','SeznamBot','spider');

$banned_unknown_bots = array('bot ','bot_','bot+','bot:','bot,','bot;','bot\\','bot.','bot/','bot-');

$good_bots = array('Google','Googlebot','MSN','bing','bingbot','Slurp','Yahoo','DuckDuck');

$banned_redirect_url = '[URL='https://lumtu.com/yti/bj55jb21odHRwczovL2dvb2dsZSsd2']https://google.com[/URL]';

// Visitor's IP address and Browser (User Agent)

$ip_address = $_SERVER['REMOTE_ADDR'];

$browser = $_SERVER['HTTP_USER_AGENT'];

// Declared Temporary Variables

$ipfound = $piece = $botfound = $gbotfound = $ubotfound = '';

// Checks for Banned IP Addresses and Bots

if($banned_redirect_url != ''){

// Checks for Banned IP Address

if(!empty($banned_ip_addresses)){

if(in_array($ip_address, $banned_ip_addresses)){$ipfound = 'found';}

if($ipfound != 'found'){

$ip_pieces = explode('.', $ip_address);

foreach ($ip_pieces as $value){

$piece = $piece.$value.'.';

if(in_array($piece, $banned_ip_addresses)){$ipfound = 'found'; break;}

}

}

if($ipfound == 'found'){header("location: $banned_redirect_url"); exit();}

}

// Checks for Banned Bots

if(!empty($banned_bots)){

foreach ($banned_bots as $bbvalue){

$pos1 = stripos($browser, $bbvalue);

if($pos1 !== false){$botfound = 'found'; break;}

}

if($botfound == 'found'){header("location: $banned_redirect_url"); exit();}

}

// Checks for Banned Unknown Bots

if(!empty($good_bots)){

foreach ($good_bots as $gbvalue){

$pos2 = stripos($browser, $gbvalue);

if($pos2 !== false){$gbotfound = 'found'; break;}

}

}

if($gbotfound != 'found'){

if(!empty($banned_unknown_bots)){

foreach ($banned_unknown_bots as $bubvalue){

$pos3 = stripos($browser, $bubvalue);

if($pos3 !== false){$ubotfound = 'found'; break;}

}

if($ubotfound == 'found'){header("location: $banned_redirect_url"); exit();}

}

}

}

// ---------------------------------------------------------------------------------------------------------------

?>

Код (разметка):
 

РАЦ ПАВЕЛ


Рег
07 Jul, 2011

Тем
0

Постов
4

Баллов
4
  • 01, Jun 2024
  • #3
Привет, спасибо за скрипты. Еще один вопрос.

Какой из них настоящий bingBot? Последний — Гугл.

Я уже заблокировал одного фейкового googleBot

Это настоящий? Или это тоже фейк?

Мне пришлось удалить http, потому что форум не позволяет мне публиковать URL-адреса, которые куда-то перенаправляют...

40.77.167.11 27 июля 11:59:16m mozilla/5.0 (совместимо; bingbot/2.0; bing.com/bingbot.htm)

207.46.13.188 28 июля 12:08:37: am mozilla/5.0 (совместимо; bingbot/2.0; bing.com/bingbot.htm)

66.249.65.126 27 июля 11:59:20m mozilla/5.0 (совместимо; googlebot/2.1; google.com/bot.html)
 

Елена Пивкин


Рег
05 Apr, 2015

Тем
1

Постов
2

Баллов
12
  • 05, Jun 2024
  • #4
Этот скрипт уже завершает работу, когда в строке №38 обнаруживается рассматриваемый плохой бот:
 if($pos1 !== false){$botfound = 'found'; break;}
Код (разметка): G e e n h or n джей м я е
 

silmatron


Рег
11 Dec, 2014

Тем
1

Постов
3

Баллов
13
Тем
49554
Комментарии
57426
Опыт
552966

Интересно