We can use HTTP_USER_AGENT to determine whether it is a spider. Search engine spiders have their own unique signs, some of which are listed below.
- function is_crawler() {
- $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
- $spiders = array(
- 'Googlebot', // Google crawler
- 'Baiduspider', // Baidu crawler
- 'Yahoo! Slurp', // Yahoo crawler
- 'YodaoBot', // Youdao crawler
- 'msnbot' // Bing crawler
- // More crawler keywords
- );
- foreach ($spiders as $spider) {
- $spider = strtolower($spider);
- if (strpos($userAgent, $spider) !== false) {
- return true;
- }
- }
- return false;
- }
Copy code
The php code below comes with more spider logos
- function isCrawler() {
- echo $agent= strtolower($_SERVER['HTTP_USER_AGENT']);
- if (!empty($agent)) {
- $ spiderSite= array(
- "TencentTraveler",
- "Baiduspider+",
- "BaiduGame",
- "Googlebot",
- "msnbot",
- "Sosospider+",
- "Sogou web spider",
- "ia_archiver",
- "Yahoo! Slurp",
- "YoudaoBot",
- "Yahoo Slurp",
- "MSNBot",
- "Java (Often spam bot)",
- "BaiDuSpider",
- "Voila",
- "Yandex bot",
- "BSpider",
- "twiceler",
- "Sogou Spider",
- "Speedy Spider",
- "Google AdSense",
- "Heritrix",
- "Python-urllib",
- "Alexa (IA Archiver)",
- "Ask",
- "Exabot",
- "Custo",
- "OutfoxBot/YodaoBot",
- "yacy",
- "SurveyBot",
- "legs",
- "lwp-trivial",
- "Nutch",
- "StackRambler",
- " The web archive (IA Archiver)",
- "Perl tool",
- "MJ12bot",
- "Netcraft",
- "MSIECrawler",
- "WGet tools",
- "larbin",
- "Fish search",
- );
- foreach($spiderSite as $val) {
- $str = strtolower($val);
- if (strpos($agent, $str) !== false) {
- return true;
- }
- }
- } else {
- return false;
- }
- }
- if (isCrawler()){
- echo "Hello Spider! ";
- }
- else{
- echo "You are not a spider spirit! ";
- }
Copy code
|