Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detect crawlers/bots/spiders in PHP (simple and fast)
<?php
/**
* Check if the given user agent string is one of a crawler, spider, or bot.
*
* @param string $user_agent
* A user agent string (e.g. Googlebot/2.1 (+http://www.google.com/bot.html))
*
* @return bool
* TRUE if the user agent is a bot, FALSE if not.
*/
function smart_ip_detect_crawler($user_agent) {
// User lowercase string for comparison.
$user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
// A list of some common words used only for bots and crawlers.
$bot_identifiers = array(
'bot',
'slurp',
'crawler',
'spider',
'curl',
'facebook',
'fetch',
);
// See if one of the identifiers is in the UA string.
foreach ($bot_identifiers as $identifier) {
if (strpos($user_agent, $identifier) !== FALSE) {
return TRUE;
}
}
return FALSE;
}
@FarrisFahad

This comment has been minimized.

Copy link

FarrisFahad commented Jan 1, 2017

How much of a proof is this code? Because I heard that bot detection cannot be 100% proof.

@FinlayDaG33k

This comment has been minimized.

Copy link

FinlayDaG33k commented Nov 16, 2017

@FarrisFahad the big problem with this code is that it does not verify the bot as legitimate.
People can just spoof the User-Agent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.