Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to detect search engine bots with PHP?
Search engine bots can be detected in PHP by analyzing the $_SERVER['HTTP_USER_AGENT'] string, which contains information about the client making the request. Bot detection is useful for serving different content or implementing specific behaviors for search engines.
Method 1: Using strstr() Function
This method checks if a specific bot name exists in the user agent string ?
<?php
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot")) {
echo "Google bot detected!";
} else {
echo "Regular user detected.";
}
?>
Code explanation − The strtolower() function converts the user agent to lowercase for case-insensitive comparison, then strstr() searches for the bot name within the string.
Method 2: Using Regular Expressions
A more comprehensive approach using preg_match() to detect multiple bot types ?
<?php
function _bot_detected() {
return (
isset($_SERVER['HTTP_USER_AGENT'])
&& preg_match('/bot|crawl|slurp|spider|mediapartners/i', $_SERVER['HTTP_USER_AGENT'])
);
}
if (_bot_detected()) {
echo "Search engine bot detected!";
} else {
echo "Human visitor detected.";
}
?>
Code explanation − The preg_match() function uses a regular expression pattern to match common bot keywords. The i flag makes the search case-insensitive.
Enhanced Bot Detection
A more detailed function that identifies specific bot types ?
<?php
function detectBot() {
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$bots = [
'googlebot' => 'Google',
'bingbot' => 'Bing',
'slurp' => 'Yahoo',
'facebookexternalhit' => 'Facebook',
'twitterbot' => 'Twitter'
];
foreach ($bots as $bot => $name) {
if (stripos($user_agent, $bot) !== false) {
return $name . ' Bot';
}
}
return false;
}
$bot = detectBot();
echo $bot ? "Detected: $bot" : "Human visitor";
?>
Conclusion
Use strstr() for simple bot detection or preg_match() with regular expressions for comprehensive checking. Always validate the $_SERVER['HTTP_USER_AGENT'] exists before processing to avoid errors.
