This question already has an answer here:
I need an if statement that will include a file on my page only if it detects crawlers from google, bing and yahoo. I found this code on another post but I want to add bing and yahoo too:
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot"))
{
//MY CODE
}
I would really appreciate if someone could solve this problem for me.
</div>
Crawler user-agents can be found here. Filtering out the ones you want should be trivial. On a sidenote, don't use strstr()
for these kind of checks, strpos()
is faster and less memory intensive. And you should typecheck against FALSE
, because either function may return a value that evaluates to false
.
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($ua, "googlebot") !== false || strpos($ua, "bingbot") !== false || … ) {
// do something
}