Skip to content

Instantly share code, notes, and snippets.

@worka
Forked from mrLexx/bot_detect.php
Last active December 10, 2019 00:25
Show Gist options
  • Save worka/56bf3ef6c7656f8188f46a49af4a9bc0 to your computer and use it in GitHub Desktop.
Save worka/56bf3ef6c7656f8188f46a49af4a9bc0 to your computer and use it in GitHub Desktop.

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'])) {
    // allowed bot
}

function botDetected()
{
    return (
        isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider|mediapartners/i', $_SERVER['HTTP_USER_AGENT'])
    );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment