欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php/asp/asp.net中判断百度移动和PC蜘蛛的实现代码

程序员文章站 2022-08-28 09:17:29
由于移动流量日趋增多,我们统计网站流量的时候,需要把移动和pc的流量分开,而遇到百度蜘蛛的时候,为了更好更细的统计,也需要把百度蜘蛛的移动端和pc端分别来统计,这对网站分析...

由于移动流量日趋增多,我们统计网站流量的时候,需要把移动和pc的流量分开,而遇到百度蜘蛛的时候,为了更好更细的统计,也需要把百度蜘蛛的移动端和pc端分别来统计,这对网站分析有着非常重要的意义。本文提供判断百度移动蜘蛛和百度pc蜘蛛的源代码,有php、asp、asp.net三个版本。

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<>"" and instr(ua,"baiduspider")<>0 then
      if instr(ua,"android")<>0 or instr(ua,"mobile")<>0 then
        response.write "是百度移动 ua"
      else
        response.write"是百度pc ua"
      end if
    else
      response.write "不是baiduspiderua"
    end if
    %>

继续为大家推荐个asp中判断是否为搜索引擎的代码:

function isspider()
dim i,agent,searray
agent="agent:"&lcase(request.servervariables("http_user_agent"))
searray=array("googlebot","baiduspider","sogouspider","yahoo","sosospider")
isspider= false
for i=0 to ubound(searray)
 if (instr(agent,searray(i))>0) then isspider=true
next
end function

asp.net

 string ua = request.servervariables["http_user_agent"];
    ua=ua.tolower ();
    if(ua != "" && ua.indexof("baiduspider") > -1){
      if (ua.indexof("android") > -1 || ua.indexof("mobile") > -1)
      {
        response .write("是百度移动 ua");
      }
      else{
        response .write("是百度pc ua");
      }
    }
    else{
      response .write("不是baiduspiderua");
    }
string agent = system.web.httpcontext.current.request.servervariables["http_user_agent"];
  if (agent == null)
  {
  response.write("other");
  }
  else
  {
  response.write(agent);
  }

以上demo是通过ua来判断来访者是否baiduspider,是比较普遍的方法。

浏览器ua大全

1、主要浏览器

safari 5.1 – mac
user-agent:mozilla/5.0 (macintosh; u; intel mac os x 10_6_8; en-us) applewebkit/534.50 (khtml, like gecko) version/5.1 safari/534.50

safari 5.1 – windows
user-agent:mozilla/5.0 (windows; u; windows nt 6.1; en-us) applewebkit/534.50 (khtml, like gecko) version/5.1 safari/534.50

ie 9.0
user-agent:mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; trident/5.0;

ie 8.0
user-agent:mozilla/4.0 (compatible; msie 8.0; windows nt 6.0; trident/4.0)

ie 7.0
user-agent:mozilla/4.0 (compatible; msie 7.0; windows nt 6.0)

ie 6.0
user-agent: mozilla/4.0 (compatible; msie 6.0; windows nt 5.1)

firefox 4.0.1 – mac
user-agent: mozilla/5.0 (macintosh; intel mac os x 10.6; rv:2.0.1) gecko/20100101 firefox/4.0.1

firefox 4.0.1 – windows
user-agent:mozilla/5.0 (windows nt 6.1; rv:2.0.1) gecko/20100101 firefox/4.0.1

opera 11.11 – mac
user-agent:opera/9.80 (macintosh; intel mac os x 10.6.8; u; en) presto/2.8.131 version/11.11

opera 11.11 – windows
user-agent:opera/9.80 (windows nt 6.1; u; en) presto/2.8.131 version/11.11

chrome 17.0 – mac
user-agent: mozilla/5.0 (macintosh; intel mac os x 10_7_0) applewebkit/535.11 (khtml, like gecko) chrome/17.0.963.56 safari/535.11

2、国产浏览器

maxthon
user-agent: mozilla/4.0 (compatible; msie 7.0; windows nt 5.1; maxthon 2.0)

tt
user-agent: mozilla/4.0 (compatible; msie 7.0; windows nt 5.1; tencenttraveler 4.0)

the world 2.x
user-agent: mozilla/4.0 (compatible; msie 7.0; windows nt 5.1)

the world 3.x
user-agent:?mozilla/4.0 (compatible; msie 7.0; windows nt 5.1; the world)

搜狗浏览器 1.x
user-agent:?mozilla/4.0 (compatible; msie 7.0; windows nt 5.1; trident/4.0; se 2.x metasr 1.0; se 2.x metasr 1.0; .net clr 2.0.50727; se 2.x metasr 1.0)

360se
user-agent: mozilla/4.0 (compatible; msie 7.0; windows nt 5.1; 360se)

avant
user-agent: mozilla/4.0 (compatible; msie 7.0; windows nt 5.1; avant browser)

green browser
user-agent: mozilla/4.0 (compatible; msie 7.0; windows nt 5.1)

3、移动设备端

safari ios 4.33 – iphone
user-agent:mozilla/5.0 (iphone; u; cpu iphone os 4_3_3 like mac os x; en-us) applewebkit/533.17.9 (khtml, like gecko) version/5.0.2 mobile/8j2 safari/6533.18.5

safari ios 4.33 – ipod touch
user-agent:mozilla/5.0 (ipod; u; cpu iphone os 4_3_3 like mac os x; en-us) applewebkit/533.17.9 (khtml, like gecko) version/5.0.2 mobile/8j2 safari/6533.18.5

safari ios 4.33 – ipad
user-agent:mozilla/5.0 (ipad; u; cpu os 4_3_3 like mac os x; en-us) applewebkit/533.17.9 (khtml, like gecko) version/5.0.2 mobile/8j2 safari/6533.18.5

android n1
user-agent: mozilla/5.0 (linux; u; android 2.3.7; en-us; nexus one build/frf91) applewebkit/533.1 (khtml, like gecko) version/4.0 mobile safari/533.1

android qq for android
user-agent: mqqbrowser/26 mozilla/5.0 (linux; u; android 2.3.7; zh-cn; mb200 build/grj22; cyanogenmod-7) applewebkit/533.1 (khtml, like gecko) version/4.0 mobile safari/533.1

android opera mobile
user-agent: opera/9.80 (android 2.3.4; linux; opera mobi/build-1107180945; u; en-gb) presto/2.8.149 version/11.10

android pad moto xoom
user-agent: mozilla/5.0 (linux; u; android 3.0; en-us; xoom build/hri39) applewebkit/534.13 (khtml, like gecko) version/4.0 safari/534.13

blackberry
user-agent: mozilla/5.0 (blackberry; u; blackberry 9800; en) applewebkit/534.1+ (khtml, like gecko) version/6.0.0.337 mobile safari/534.1+

webos hp touchpad
user-agent: mozilla/5.0 (hp-tablet; linux; hpwos/3.0.0; u; en-us) applewebkit/534.6 (khtml, like gecko) wosbrowser/233.70 safari/534.6 touchpad/1.0

nokia n97
user-agent: mozilla/5.0 (symbianos/9.4; series60/5.0 nokian97-1/20.0.019; profile/midp-2.1 configuration/cldc-1.1) applewebkit/525 (khtml, like gecko) browserng/7.1.18124

windows phone mango
user-agent: mozilla/5.0 (compatible; msie 9.0; windows phone os 7.5; trident/5.0; iemobile/9.0; htc; titan)

uc标准
user-agent: nokia5700/ ucweb7.0.2.37/28/999

ucopenwave
user-agent: openwave/ ucweb7.0.2.37/28/999

uc opera
user-agent: mozilla/4.0 (compatible; msie 6.0; ) opera/ucweb7.0.2.37/28/999