Skip to content
PHP Logo

How can a PHP developer prevent search engines from increasing download counters on their website?

To prevent search engines from increasing download counters on a website, a PHP developer can check the user agent of the incoming request and only increment the download counter if the request is not from a known search engine bot. This can be achieved by comparing the user agent string with a list of known search engine bot user agents.

php
$userAgent = $_SERVER['HTTP_USER_AGENT'];

$searchEngineBots = array(
    'Googlebot',
    'Bingbot',
    'Yahoo! Slurp',
    'DuckDuckBot',
    // Add more search engine bot user agents as needed
);

$isSearchEngineBot = false;
foreach ($searchEngineBots as $bot) {
    if (strpos($userAgent, $bot) !== false) {
        $isSearchEngineBot = true;
        break;
    }
}

if (!$isSearchEngineBot) {
    // Increment download counter here
}