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.         ?>
复制代码

% F$ a; g8 e7 J5 K% p' S/ T. o

% I. w6 `2 `' M; N+ o

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.         %>
复制代码

+ i6 K4 ^0 c: M) G( i" ~7 G+ b& d

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.         }
复制代码
7 b# \. `0 h1 ]/ x1 _6 {

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

6 S5 r9 t. Q2 n9 c& U
5 X6 [2 f- i6 z% q

浏览器UA大全

1、主要浏览器

safari 5.1 – MAC
. A0 j& Q1 _1 Y) `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
) {3 L# v: R' |9 U! ~( r3 I0 s: M3 `, }3 D+ y0 x
safari 5.1 – Windows, |( k8 l" z# D$ T" _0 C
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( R: |# s( c- y5 Y  V
' F; g) I, P- l# A+ l, I
IE 9.0
4 }" p. E' h4 t) cUser-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;, p# ~/ P9 O) k  Y

1 `' M2 o! H% F6 L! {7 ^& K& eIE 8.0
* }$ W2 j  C# h9 Z" I7 Q9 \User-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)
" v% N! b! y$ e) s* Y8 y1 t8 L& t
IE 7.0
) y1 f  y1 v: T- kUser-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)
5 ?* g- O. r( y# Y# {
, [9 c2 S* c2 Y9 |1 E$ z3 xIE 6.0
) X, ^# f# v" `& Y" AUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)6 X! ^0 ^* {2 a+ \
1 `( z/ U+ h* q' r1 H
Firefox 4.0.1 – MAC
) a$ e7 H9 b) ]0 Y( A. `User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
- o7 ^. |- n% J7 @! T, k) }4 y+ a$ U' S
Firefox 4.0.1 – Windows
- [* v+ T0 Z" T' G$ h% f3 ^User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1" L- ~* N1 s  f/ K

# {* w+ q1 v. dOpera 11.11 – MAC
( W& M% `3 T( ?' t: I. D1 VUser-Agent:Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11& m9 A+ v& F: q% U
1 S7 c; j0 j4 C+ {$ B3 U
Opera 11.11 – Windows
5 f4 ]8 H7 h* s% IUser-Agent:Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11
8 y0 N, ]# d; j3 B7 B- |) y( o- H; U1 O
Chrome 17.0 – MAC
5 c4 k" ?8 T* A; n/ _- Y# uUser-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) n% `4 j" V! B# \, }# u
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)
% L! s4 ?* i& N: @! G, O* r8 ~5 d5 P9 j8 s% _
TT
2 j0 y6 z* ?( gUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)% S; B" r: P7 Q1 _: m

: V* @% ^" _! {2 K* S1 v% k; k4 bThe World 2.x) y% Q, l0 V& m, y) _
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)
& Z6 _9 U! W$ l6 k6 L" ~3 m; g. }9 n1 @
The World 3.x
- u0 z  ], p2 s5 f' L! XUser-Agent:?Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)
9 D7 G; R2 e8 B# }6 h
* w( Y; i7 t! n+ Q: {, x2 P搜狗浏览器 1.x
: M# E6 s. Z) TUser-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)* V% b" Y6 H; V1 {
6 J6 _$ w$ H  `3 R7 O& B" s
360SE
& R3 d4 I$ v+ T, ]4 W5 RUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)
% R, u! i, d7 K/ v. O9 ^& l5 ~: {- Q
Avant* U- f" }! f( `  D& @4 {" i
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)5 L# N. ~% W/ K) `2 u
( B7 t( F" }4 s" `2 j% @3 j5 Q
Green Browser. }6 N, x% x6 B! i
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)

3、移动设备端

safari iOS 4.33 – iPhone
) T0 G! h! Z  wUser-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. S4 I, x' B9 w% d
0 P0 {7 H7 H; T. ~9 {
safari iOS 4.33 – iPod Touch
# m. ?1 A3 k8 h  G' a& ?0 [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.51 c* ^& N1 |" ^# W
$ d  ?, y% _. w" C6 F6 w
safari iOS 4.33 – iPad
( a5 @2 X3 a, I, F/ s' Z0 UUser-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.55 P7 D  m- _! T' }& p

) y  N1 |, P* J$ @( D9 YAndroid N1
" }1 q; }9 B9 @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.11 N) D0 `& @* O8 g& U, [. d7 k6 B
+ ?7 ?* E% _. m* F9 R  a1 k+ i
Android QQ For android* D1 I  y2 G. c6 K' l( W
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
+ }# `5 Z8 V& q' h  m! V- l! k+ C! v; G5 I
Android Opera Mobile& F8 g4 [" U, W( y5 A- J
User-Agent: Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10
9 Z0 y6 R7 P' m( f1 @; G6 A" J* ^+ S1 f7 C$ x6 S  A
Android Pad Moto Xoom9 p5 G* z  ]* W4 v( d: d
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
9 l( m  G8 w5 m$ l  \& n& U: P
5 B  j6 _! z% N' m( R& s: Y6 i. bBlackBerry7 q) X& Q  q1 }0 @+ q; [: d: l7 D
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+; _3 ?2 {1 g3 C' J- ]4 B
) k8 c* S8 f9 ~& Y% {6 R: l2 J) K! `
WebOS HP Touchpad+ _, v! ~! e0 u0 u/ k
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
% P4 f  E2 j) K) f% _# Z2 \& c6 g: i' H* S8 X
Nokia N97! g0 j3 m! I% _  k
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
3 X1 M' m) m4 Z* H6 I$ K) ~( L% g3 x  j+ z7 g0 o$ I5 h1 V1 F
Windows Phone Mango' z" v% @' K3 ]4 P
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)0 P% q1 h" {1 G3 K' m3 `1 d
& x4 j4 U9 m! w* w8 q6 g
UC标准
9 t+ V, F) n* k3 l. ZUser-Agent: NOKIA5700/ UCWEB7.0.2.37/28/999
& Y6 w6 g0 V$ o( Q7 ^) @+ N! `
; {2 D( m# ?7 _" P" S$ B9 }7 tUCOpenwave
4 p, r. s: H  _7 Z0 p( D4 \User-Agent: Openwave/ UCWEB7.0.2.37/28/999
+ d; D' e7 ~4 }, ^0 A# b
3 W' l, V' P; ^1 I5 JUC Opera: Q4 z( y9 M( [8 O$ }
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