Board logo

标题: [其它] 分享php/asp/asp.net aspx判断百度PC端和移动端蜘蛛的源代码 [打印本页]

作者: jfx    时间: 2016-8-30 17:38     标题: 分享php/asp/asp.net aspx判断百度PC端和移动端蜘蛛的源代码

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

php版

  1. <?php
  2.         $ua=$_SERVER['HTTP_USER_AGENT'];
  3.         $ua=strtolower($ua);
  4.         if($ua!="" && strpos($ua,"baiduspider")){
  5.             if(strpos($ua,"android") || strpos($ua,"mobile")){
  6.                 echo "是百度移动UA";
  7.             }else{
  8.                 echo "是百度PCUA";
  9.             }
  10.         } else{
  11.               echo"不是baiduspider UA";
  12.         }
  13.         ?>
复制代码
2 i1 r* M. P3 r% D  _' g


* s, y2 Y  \# [

asp版

  1. <%
  2.         ua=Request.ServerVariables("HTTP_USER_AGENT")
  3.         ua=LCase(ua)
  4.         if ua<>"" and InStr(ua,"baiduspider")<>0 then
  5.             if InStr(ua,"android")<>0 or InStr(ua,"mobile")<>0 then
  6.                 response.write "是百度移动 UA"
  7.             else
  8.                response.write"是百度PC UA"
  9.             end if
  10.         else
  11.             response.write "不是baiduspiderUA"
  12.         end if
  13.         %>
复制代码

: f+ I" m4 E3 y* a3 Y$ O# {

asp.net版

  1. string ua = Request.ServerVariables["HTTP_USER_AGENT"];
  2.         ua=ua.ToLower ();
  3.         if(ua != "" && ua.IndexOf("baiduspider") > -1){
  4.             if (ua.IndexOf("android") > -1 || ua.IndexOf("mobile") > -1)
  5.             {
  6.                 Response .Write("是百度移动 UA");
  7.             }
  8.             else{
  9.                 Response .Write("是百度PC UA");
  10.             }
  11.         }
  12.         else{
  13.             Response .Write("不是baiduspiderUA");
  14.         }
复制代码

" c; ?% _' j$ d0 v

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


: f4 c6 `% y; g" y0 X  @9 V+ u* [+ b+ K/ I4 e& R  \. W- U

浏览器UA大全

1、主要浏览器

safari 5.1 – MAC
/ ?$ S8 G9 J- c' LUser-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
. C1 H0 E! n3 ]5 i/ z+ ]3 X9 R0 L! l& C0 N
safari 5.1 – Windows
. P; M6 a& j: F) C+ A+ Q% o. `, JUser-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.500 ^( Z' P) I5 C9 m9 ?& e. p; ^& ~

5 ?% ?9 r5 d6 J* F8 sIE 9.0
" k; ^4 J; \- ?# U+ `+ DUser-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;/ b( O7 V$ |/ ?) x9 H
5 _6 X5 C$ x; r) V! e; B
IE 8.0# l! U; G6 N0 m  H, P# q
User-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)/ H7 \- P- N8 z

/ w  ^4 ]* P% @8 m8 ZIE 7.0
. m) n: y4 Y0 w, \* U3 a4 N( W: x6 o/ pUser-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)' ?3 _; K( i/ P! p
6 z: P" l6 @/ C- w( y1 s# F
IE 6.0, J3 o# {. ~8 K- X1 H
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
+ I/ v5 l  x) ]1 M+ `+ L6 H
2 S4 q. e7 e1 g" _Firefox 4.0.1 – MAC
# ?5 f5 G% x& s+ e$ b4 jUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.18 \* w9 ?  S' m, k$ q
; W  ^, n( U& O1 P9 ]: U; ^  @
Firefox 4.0.1 – Windows
* M6 n+ p4 f# I7 v8 rUser-Agent:Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1% v" E; S. o- y

$ V9 w: J) _3 cOpera 11.11 – MAC7 C1 H; N" d1 q4 g# q3 J& k- p
User-Agent:Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11
3 K  S& C9 u  c
7 [  n  y0 r# B, H! {5 iOpera 11.11 – Windows
2 T% D: I* _: `/ Y- FUser-Agent:Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11) o0 `9 u( _: }8 i

& C' i9 E% a+ o; jChrome 17.0 – MAC
1 C4 c- |% [2 G, t- AUser-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
5 D3 S! |: l! LUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)6 q+ x( Z: g' E" G' u' I, R4 H

* W) t# y" L& w9 e1 o. y4 bTT6 n. H' b! i0 ~1 d0 y
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)5 D0 o! t5 m: r! m) u

) k- [- x, H; `# I. ?$ @The World 2.x
; S: h3 t# U- gUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)
" P1 N* X; A4 a
$ j% S+ }  W5 I* XThe World 3.x7 Z2 Z: S! @% Y- g; W
User-Agent:?Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)9 |1 O2 [  |, A1 }6 a; o, O% Z

$ \% f8 Q+ n5 J; A2 ]/ x+ [搜狗浏览器 1.x
4 L6 d4 R6 d/ s  {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), q: Q# V4 n8 Z0 ]: I
( q9 j! U% J( ~) i- B0 c3 A! Y; t% M
360SE
2 w  n  f4 A$ j9 fUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)
* s" @/ d7 v# v  i
. y4 w5 V2 ~5 p0 V2 JAvant; q% l: M4 e& P+ f# [: Z) _! i
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)
: y4 P' f: k. p" e2 T4 r: N/ M/ p
Green Browser" D: ]- I+ B' \2 G( X5 q
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)

3、移动设备端

safari iOS 4.33 – iPhone
5 U5 H% {2 o. Y$ T. e; ]. D% cUser-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
1 R3 i1 u; ]- d% `8 W7 ?+ w' _
& u# a0 Q) w0 ^5 K+ ]/ Xsafari iOS 4.33 – iPod Touch
0 @; j% c7 k5 k6 C% f0 y  F. V  }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
& A' @! G+ ]+ o+ q4 s3 S  e) d% ?$ u! _! [: \$ `
safari iOS 4.33 – iPad
9 x' q: Y1 U) h& c, ~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
& p: K3 Q1 a- i* d1 C
  _7 ^; r# D( n4 `9 BAndroid N1
% q, J8 E6 S, U  ?( b5 I; ]8 }! tUser-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.17 u# E/ ^- q; e; P* C+ q4 k' c* ~
) d) `& r, d$ S0 V0 ^+ }
Android QQ For android
$ b( S, M; }# M/ EUser-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
& ~, z* k- K9 h( O; s+ W' g6 m1 o# F% j; N- _! [- }0 D! Z
Android Opera Mobile% q' s% }  U- ?. Z3 A
User-Agent: Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10
) G% M1 `2 V2 N1 F# A# z; j, U: H$ Q# J& [, e7 k
Android Pad Moto Xoom, v& ^2 `# Y& V: H  X: b2 M: \
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.132 K7 r5 _1 _3 a
( c$ X* q2 V: R  K5 @
BlackBerry, X3 ?! i! M; J  B% R' x$ D$ Y
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+8 w& Q. M9 u  a# g: I" [, Y5 `

  {& p5 }( q$ S  c& G4 hWebOS HP Touchpad7 p% p; ^* {1 R6 a/ V! m, u) [
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.08 y: a/ d4 ^/ I0 f/ L" V" c5 R

4 s: N. z; ^: r) m" y( \. t# g5 yNokia N97
; ~5 N$ `0 }" S+ p7 G3 c! [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+ r6 F9 e+ D6 l- G. g7 H

3 N  z: w3 ~- U& \Windows Phone Mango( ]9 @* z- Q5 C0 Z8 a, K4 c
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)
9 i: d" ~$ Q; v2 i* f+ x
! `3 k. E& e" t* E. q7 _' |UC标准# m: Q* k( Y9 c8 w( v
User-Agent: NOKIA5700/ UCWEB7.0.2.37/28/999
; P. f: t# f7 Z0 F) |& E+ f% J6 z0 m% G" }+ Q% K; V( I. p
UCOpenwave
( O. X0 V% p" e% nUser-Agent: Openwave/ UCWEB7.0.2.37/28/9994 |/ x0 r8 d/ C9 y# C0 }

5 O. M  L1 i% H+ _) |" ?+ x9 T3 {UC Opera7 T$ }, K5 E+ z+ D$ B, Z
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/999

参考: http://www.webkaka.com/tutorial/zhanzhang/2015/060421/






欢迎光临 捷飞网络论坛 (http://bbs.fjbjdd.com/) Powered by Discuz! 7.2