Skip to content

Instantly share code, notes, and snippets.

@worka
worka / detectBot.md
Last active December 10, 2019 00:25 — forked from mrLexx/bot_detect.php

Detect bot

http://qaru.site/questions/60076/how-to-detect-search-engine-bots-with-php

if (preg_match('/bot|crawl|curl|dataprovider|search|get|spider|find|java|majesticsEO|google|yahoo|teoma|contaxe|yandex|libwww-perl|facebookexternalhit/i', $_SERVER['HTTP_USER_AGENT'])) {
    // is bot
}

if (preg_match('/apple|baidu|bingbot|facebookexternalhit|googlebot|-google|ia_archiver|msnbot|naverbot|pingdom|seznambot|slurp|teoma|twitter|yandex|yeti/i', $_SERVER['HTTP_USER_AGENT'])) {

Detect google and yandex search bots (crawl, spider)

function detectSearchBot($ip, $agent, &$hostname)
{
    $hostname = $ip;

    // check HTTP_USER_AGENT what not to touch gethostbyaddr in vain
    if (preg_match('/(?:google|yandex)bot/iu', $agent)) {
 // success - return host, fail - return ip or false