How to detect robots and spiders with PHP?

To detect robots and spiders (web crawlers) using PHP, you can examine the user agent provided in the HTTP request headers. Web crawlers often identify themselves by specific user agents. Here's a step-by-step guide on how to detect them:

1. Access User Agent:

   The user agent is part of the HTTP headers sent by the client (crawler). In PHP, you can access it through the `$_SERVER` superglobal:

   $userAgent = $_SERVER['HTTP_USER_AGENT'];

2. Create a List of Known User Agents:

   To detect crawlers, you need to maintain a list of known user agents used by common web crawlers. These user agents are often well-documented and can be found on the official websites of major search engines like Google, Bing, and others.

   Here are a few examples of common user agents:

   - Googlebot: `Googlebot`

   - Bingbot: `Bingbot`

   - Yahoo Slurp: `Yahoo! Slurp`

   - Baiduspider (Baidu): `Baiduspider`

   - Yandex: `YandexBot`

3. Check User Agent:

   Compare the received user agent with the list of known user agents to detect if it's a web crawler:

   $knownUserAgents = [



       'Yahoo! Slurp',



       // Add more web crawler user agents here


   $isCrawler = false;

   foreach ($knownUserAgents as $crawlerUserAgent) {

       if (stripos($userAgent, $crawlerUserAgent) !== false) {

           $isCrawler = true;




   The `stripos` function is used with the `stripos($userAgent, $crawlerUserAgent)` call to perform a case-insensitive search for the crawler's user agent within the user agent string.

4. Handle the Detection:

   Once you have checked if the user agent matches any known web crawler user agents, you can perform actions accordingly. For example, you might log crawler visits, restrict access to certain content, or customize the response for crawlers:

   if ($isCrawler) {

       // This is a web crawler

       // Perform actions for web crawlers

   } else {

       // This is a regular user

       // Serve content as usual


   You can customize this logic based on your specific requirements. For example, you might want to return a `robots.txt` file for web crawlers, display a different version of your website, or log crawler activity.

5. Regularly Update the List:

   It's essential to keep your list of known user agents up-to-date, as new crawlers may emerge, and existing ones may change their user agent strings.

6. Testing and Handling Edge Cases:

   Test your implementation thoroughly with various user agents to ensure accurate detection. Additionally, handle edge cases gracefully, such as cases where the user agent header is missing.

Remember that some malicious bots or scrapers might not follow standard user agent conventions. Therefore, user agent-based detection is not foolproof, and additional techniques like rate limiting or captcha checks may be necessary to protect your website from unwanted automated access.