本文发布于:2012-12-20,最后更新于:2021-02-09,如果内容失效请留言告知。
有时我们需要对网站访客来路进行识别,针对真实用户与搜索引擎作不同动作实现,那么首先就需要判断是否为搜索引擎。php判断方法非常简单,通过过滤$_SERVER['HTTP_USER_AGENT'] 参数即可进行识别,以下是摘录某开源程序的相关源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | private function getRobot() { if ( empty ( $_SERVER [ 'HTTP_USER_AGENT' ])) { return false; } $searchEngineBot = array ( 'googlebot' => 'google' , 'mediapartners-google' => 'google' , 'baiduspider' => 'baidu' , 'msnbot' => 'msn' , 'yodaobot' => 'yodao' , 'youdaobot' => 'yodao' , 'yahoo! slurp' => 'yahoo' , 'yahoo! slurp china' => 'yahoo' , 'iaskspider' => 'iask' , 'sogou web spider' => 'sogou' , 'sogou push spider' => 'sogou' , 'sosospider' => 'soso' , 'spider' => 'other' , 'crawler' => 'other' , ); $spider = strtolower ( $_SERVER [ 'HTTP_USER_AGENT' ]); foreach ( $searchEngineBot as $key => $value ) { if ( strpos ( $spider , $key )!== false) { return $value ; } } return false; } public function isRobot() { if ( $this ->getRobot()!==false) { return true; } return false; } |
那要不是百度google等著名引擎的蜘蛛 怎么判断呢?比如 我做的一个bt搜索站 http://bt.pekdo.com 怎么判断类似这样小站的的蜘蛛?
[reply=任侠,2013-02-21 12:57 AM]搜索引擎的蜘蛛一般都会在http头里添加独特的标识,其他类型的蜘蛛可以'spider'=>'other', 'crawler'=>'other', 来进行判断。至于你说的搜索站,不是标准的搜索引擎,在未知其信息的情况下,只能当做普通的来源网站处理[/reply]