How can I as in PHP if a client is a search engine bot?

asked 2023-04-26 11:13:44 +0000

wagner gravatar image

To create a PHP function that checks if the client is a search engine bot, you can analyze the User-Agent string in the HTTP request headers. The following function checks for some of the major search engine bots, including Googlebot, Bingbot, Yahoo! Slurp, Baidu Spider, and Yandex Bot:

function isSearchEngineBot() {
    $userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';

    // Define a list of known search engine bot patterns
    $botPatterns = [
        'Yahoo! Slurp',

    // Check if the User-Agent string matches any known bot patterns
    foreach ($botPatterns as $botPattern) {
        if (stripos($userAgent, $botPattern) !== false) {
            return true;

    return false;

This function retrieves the User-Agent string from the $_SERVER global variable and checks if it contains any known search engine bot patterns. It returns true if a match is found, and false otherwise. Keep in mind that the User-Agent can be easily spoofed, so this method is not foolproof. However, it should suffice for most use cases.

edit retag flag offensive close merge delete