如何用程序识别Baiduspiderua

近日 ,Baiduspider针对移动抓取user agent(以下简称ua)进行了升级,与PC端的抓取ua做到版本统一,均称为Baiduspider/2.0。从此次更新的移动ua和PC ua来看,不管是移动ua还是PC ua都包含有关键字Baiduspider,我们可以由此判断访客ua是不是来自百度。与PC ua不同的是,移动ua包含有关键字android和mobile,再通过这两个关键字,我们就可以识别出来访的访客ua是移动ua还是pc ua。
通过以上判断逻辑,我们可以很容易判断出来访的ua类型。下面是如何通过主流的php和asp语言来判断来访的ua类型。
php版:
<?php$ua=$_SERVER['HTTP_USER_AGENT'];$ua=strtolower($ua);if($ua!="" &&strpos($ua,"baiduspider")){if(strpos($ua,"android") || strpos($ua,"mobile")){ echo "是百度移动UA"; }else{ echo "是百度PCUA"; }}else{ echo"不是baiduspider UA";}?>
asp版:<%ua=Request.ServerVariables("HTTP_USER_AGENT")ua=LCase(ua)if ua<>"" andInStr(ua,"baiduspider")<>0 thenifInStr(ua,"android")<>0 or InStr(ua,"mobile")<>0then response.write "是百度移动 UA" else response.write"是百度PC UA" endifelse response.write "不是baiduspiderUA"end if%>
以上demo可以用来简单判断来访ua是否是Baiduspiderua,以及是什么类型的ua,在实际使用过程中,请稍加修改后使用。


相关文章:
相关推荐:




