共计 1020 个字符,预计需要花费 3 分钟才能阅读完成。
提醒:本文最后更新于2021-02-09 08:59,文中所关联的信息可能已发生改变,请知悉!
有时我们需要对网站访客来路进行识别,针对真实用户与搜索引擎作不同动作实现,那么首先就需要判断是否为搜索引擎。php判断方法非常简单,通过过滤$_SERVER['HTTP_USER_AGENT'] 参数即可进行识别,以下是摘录某开源程序的相关源码:
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; | |
} |
正文完