hello@unluckypete.com | +421 907 149 059

PHP: How to detect bot user agents

This regular expression checks if the request came from a regular browser (or someone presenting with one), or if the request was made by a bot or crawler. I know my websites don't have any credible traffic from China, so I assume all these request are coming from bots.

function isBot() {
    return (
        isset($_SERVER['HTTP_USER_AGENT'])
        && preg_match('/bot|crawl|slurp|spider|mediapartners|Mb2345Browser|LieBaoFast|MicroMessenger|zh-CN|zh_CN|Kinza/i', $_SERVER['HTTP_USER_AGENT'])
    );
}

Published 06.12.2019 by Peter Pech

Leave a Reply