首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何用php检测搜索引擎机器人?

如何用php检测搜索引擎机器人?
EN

Stack Overflow用户
提问于 2009-03-24 13:34:41
回答 14查看 143.5K关注 0票数 141

如何使用php检测搜索引擎机器人?

EN

回答 14

Stack Overflow用户

回答已采纳

发布于 2009-03-24 13:37:22

这是一个Search Engine Directory of Spider names

然后使用$_SERVER['HTTP_USER_AGENT'];检查代理是否是所说的爬行器。

代码语言:javascript
复制
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot"))
{
    // what to do
}
票数 84
EN

Stack Overflow用户

发布于 2009-03-24 13:37:42

检查$_SERVER['HTTP_USER_AGENT']中是否存在以下列出的一些字符串:

http://www.useragentstring.com/pages/useragentstring.php

或者更具体地说,对于爬虫:

http://www.useragentstring.com/pages/useragentstring.php?typ=Crawler

如果您想-比如说-记录大多数常见搜索引擎爬虫的访问次数,您可以使用

代码语言:javascript
复制
$interestingCrawlers = array( 'google', 'yahoo' );
$pattern = '/(' . implode('|', $interestingCrawlers) .')/';
$matches = array();
$numMatches = preg_match($pattern, strtolower($_SERVER['HTTP_USER_AGENT']), $matches, 'i');
if($numMatches > 0) // Found a match
{
  // $matches[1] contains an array of all text matches to either 'google' or 'yahoo'
}
票数 20
EN

Stack Overflow用户

发布于 2013-07-18 08:53:22

你可以用这个功能检查它是否是一个搜索引擎:

代码语言:javascript
复制
<?php
function crawlerDetect($USER_AGENT)
{
$crawlers = array(
'Google' => 'Google',
'MSN' => 'msnbot',
      'Rambler' => 'Rambler',
      'Yahoo' => 'Yahoo',
      'AbachoBOT' => 'AbachoBOT',
      'accoona' => 'Accoona',
      'AcoiRobot' => 'AcoiRobot',
      'ASPSeek' => 'ASPSeek',
      'CrocCrawler' => 'CrocCrawler',
      'Dumbot' => 'Dumbot',
      'FAST-WebCrawler' => 'FAST-WebCrawler',
      'GeonaBot' => 'GeonaBot',
      'Gigabot' => 'Gigabot',
      'Lycos spider' => 'Lycos',
      'MSRBOT' => 'MSRBOT',
      'Altavista robot' => 'Scooter',
      'AltaVista robot' => 'Altavista',
      'ID-Search Bot' => 'IDBot',
      'eStyle Bot' => 'eStyle',
      'Scrubby robot' => 'Scrubby',
      'Facebook' => 'facebookexternalhit',
  );
  // to get crawlers string used in function uncomment it
  // it is better to save it in string than use implode every time
  // global $crawlers
   $crawlers_agents = implode('|',$crawlers);
  if (strpos($crawlers_agents, $USER_AGENT) === false)
      return false;
    else {
    return TRUE;
    }
}
?>

然后你可以像这样使用它:

代码语言:javascript
复制
<?php $USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
  if(crawlerDetect($USER_AGENT)) return "no need to lang redirection";?>
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/677419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档