Asked 3 years ago
21 May 2021
Views 503
jagdish

jagdish posted

How to find bot request in php ?

in PHP based website, there are many bots, the crawler is crawling the website, so I want to get detail is there request coming from a bot or is it a real user , is that possible i can get is that bot is surfing the website or real use using it ?
dilip

dilip
answered Nov 30 '-1 00:00


$user_agent=$_SERVER['HTTP_USER_AGENT'];
if (preg_match('/bot|crawl|curl|dataprovider|search|get|spider|find|java|majesticsEO|google|yahoo|teoma|contaxe|yandex|libwww-perl|facebookexternalhit/i', $user_agent)) 
{
echo "i am bot";
}


above preg match function will help to find the request come from the bot or user
this is not a full-proof method, because there is many crawler which don't define that detail in HTTP_USER_AGENT so you need to keep changing it as per latest need
sandip

sandip
answered Nov 30 '-1 00:00

if bot had made request to the page than $_SERVER['HTTP_USER_AGENT'] contain like this value :
Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)
Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.97 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)


so to parse it , following is the code will print the bot name :

if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "bot"))
{
   $a=explode(";",$stats['user_agent']);
        if($a[count($a)-2]!=''){ 
          echo $a[count($a)-2];
        }
        else {
          $a=explode(" ",$stats['user_agent']);
          if($a[count($a)-2]!=''){ 
          echo $a[count($a)-2];
          }else{ 

            echo "bot";
          }
  }




Post Answer