php - Detect if a page is visited by a bot

181

Is there a way to detect if the page is visited by a bot?

I tried checking the$_SERVER['HTTP_USER_AGENT'] is within an array. It works fine.

$bot = array("Slurp", "Scooter", "URL_Spider_SQL", "Googlebot", "Firefly", "WebBug", "WebFindBot", "crawler",  "appie", "msnbot", "InfoSeek", "FAST", "Spade", "NationalDirectory",);

if (in_array($_SERVER['HTTP_USER_AGENT'], $bot)) {
    return true;
}
else {
return false;
}

Is there a better and secured way to do this? (other than having to type-in all the bot names?) What's the difference between my method and this?

780

Answer

Solution:

Well, after some digging inside the Google I found this.

$agent = strpos(strtolower($_SERVER['HTTP_USER_AGENT']));
foreach($bots as $name => $bot)
{
    if(stripos($agent,$bot)!==false)
    {
        return true;
    }
    else {
        return false;
    }
}

Thanks for the support Dale!!

116

Answer

Solution:

Looking the Sid's answer, and googling i found on this site other way to detect. look:

function detect_is_bot () {
    $bots = array("Slurp", "Scooter", "URL_Spider_SQL", "Googlebot", "Firefly", "WebBug", "WebFindBot", "crawler",  "appie", "msnbot", "InfoSeek", "FAST", "Spade", "NationalDirectory",);
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    foreach($bots as $bot) {
        if(stripos($agent,$bot)!==false) {return true;}
    }
    return false;
}

People are also looking for solutions to the problem: advice on building php based file manager

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.