How to detect search engine bots with PHP?

Here, we are going to learn how to detect search engine bots with PHP?
Submitted by Sayesha Singh, on August 31, 2020

Search engine bots are also known by names such as spiders and crawlers. Their function is to go to web pages and search for links to the next pages and then go to them. Since they are crawling from one web page to another they are named crawlers. But why are they searching and visiting the next pages? Because they are trying to make a map of content that might turn out useful later when searching. Further, the problems and weaknesses of a website can be recognized by the developer this way.

For detecting the search engine bots in PHP, there is no as such any in-built function provided. So, we need to create our own function to perform the task.

Program:

<?php 
    function detecting_bot($system) {
      if(isset($system)  &&  preg_match('/Googlebot|Twitterbot|crawl|ia_archiver|Yahoo! slurp|facebookexternalhit|Baiduspider|mediapartners/i', $system ))
        {
            echo ("detected \n");
        }
        else
        echo("not detected \n");
    }
    
    echo detecting_bot('Googlebot'); 
    echo detecting_bot('hellobot');
?> 

Output:

detected
not detected





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.