$7.49 .com domain with free InstantPage Website Builder$1.99 Web Hosting   捷飞网络官方淘宝店   Godaddy 优惠码
返回列表 发帖
分享到:




[其它] 分享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.         ?>
复制代码

* d5 G, @/ D# r: N3 v+ w6 |9 U- U- ^

8 C1 V  ?  _, o- S* _

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.         %>
复制代码
9 I' S9 C4 s% ]6 Q

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.         }
复制代码
; ^9 T) Q4 [& o  C+ W  j3 o& W

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

9 }, A0 O( A, R

( |" Q9 U& Y/ k& Y- I3 Z: a' ^

浏览器UA大全

1、主要浏览器

safari 5.1 – MAC! }# l# h, |2 L
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
* @! J8 w. m: P1 _! Z7 i
# I" G% V' K7 X3 H  [safari 5.1 – Windows$ f' a6 c" P# m+ U. t
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
. q( B8 ~8 e* B8 |& t3 \1 h' F& d
) p6 ?# _( V& r3 y( [IE 9.0
- b9 I- j& `+ b8 IUser-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;; }6 S; ]" n5 A, i; c8 I
6 O, K2 v, R* n9 |
IE 8.05 U/ W. z; a5 r
User-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)% f1 D% x. w' O% K

( M  R4 q' q7 d; j( c5 LIE 7.0
4 m- p: [5 B8 {8 o% @7 `User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)1 V. L: b7 X# X

1 ^, z- p& a+ F& e2 TIE 6.01 N& R3 _' X* s. i& ]
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)$ V; C# ?( w) y* G# e6 Y

5 ^  a: g! ~  n: @1 |) lFirefox 4.0.1 – MAC4 S) C9 V6 j3 d9 U/ N
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1. `0 a+ q! S  K* F( f+ Q5 k# u

$ Y$ e/ ]  v+ F/ n1 B- V. PFirefox 4.0.1 – Windows
3 d" N+ o6 b$ k. k  n4 C9 t( PUser-Agent:Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
% Q8 E' e5 l6 ~
) \3 B. A7 U4 ^  u" f- s3 D: NOpera 11.11 – MAC! C5 _& [% ]& h$ ~1 b
User-Agent:Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11
8 {) @# x% G) [+ X; [. V0 k
$ }% _: o, K6 A2 b0 c. o6 Z/ QOpera 11.11 – Windows- |- z5 C) p$ C) Q" d" f
User-Agent:Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11
, L4 n4 L$ L$ o% ~; D
3 M6 H) N* `9 u$ f% ]( dChrome 17.0 – MAC
; \7 }- e( ]/ r! R- ]# t+ }/ MUser-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、国产浏览器

Maxthon2 \! b" L. i( ~+ a( u
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)
$ ~2 f( k* _0 S1 H: q# y' h! d+ ~5 M* M$ ~/ Z- O0 j' h2 B/ u
TT; \9 G% Y$ s  y" ~& f
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)
# B: E) s$ ]$ K* B6 b% H' S' S9 V
The World 2.x
! i+ H( \" T) I( j8 nUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)) p( `7 o4 ?' t2 |* z! ]! F

$ l" @- ]2 t; y! J$ X1 Q" CThe World 3.x
  r4 L, o& @' e" R+ SUser-Agent:?Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World). k/ m+ z, ?) h% G6 o" r% a

& d4 z/ B- u5 u. Q* |$ r+ l搜狗浏览器 1.x
6 a: @7 q$ z2 T6 J8 ]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)& F- I5 E; ?, {0 r3 f; t  t" ]" G
2 K  E# n3 m1 C* u
360SE
( V  Z! h: P+ m8 d7 c" r' n" `$ gUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)
; r( G. M- O1 [1 ~$ c: [8 C6 p' K4 i3 q. Z/ C4 r% l* U0 {
Avant, a/ ^2 `/ G% e6 A* T
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)
$ i6 W+ w! s# j* _3 e9 O* l
/ p! m8 T- Z8 l- l+ H9 U. [Green Browser
( m/ ]. o4 }( b' u  b7 [User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)

3、移动设备端

safari iOS 4.33 – iPhone6 _( H: F# ~3 j
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
1 X. J, ?: a5 u' \) }3 F" i
  ^. S1 G" k/ U3 t3 Wsafari iOS 4.33 – iPod Touch
) v3 c7 J6 ~( ^7 e0 \9 AUser-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' F- d' \' R: h1 C- f6 E% }6 f+ W
& g4 g2 i/ `  |2 R7 D3 C
safari iOS 4.33 – iPad+ I5 Q/ H. V+ n3 Q$ `
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
9 X7 t, C6 h' h# v, I) X" e  f) J9 a6 e1 `+ F6 A9 }8 ~
Android N1
4 ?: p% k, g  M* s7 |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+ e, R) I8 l6 V& ~

% g6 @, J: D2 a7 \/ H- Z: IAndroid QQ For android4 O# F% |! @1 L; y4 m0 a
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/ D6 Q" Z5 a1 L
3 a" {  D& c0 X( I# Y
Android Opera Mobile% Q& ^0 ]+ @, V
User-Agent: Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10' v; |$ c& z5 U8 ?9 I; c

/ u9 R9 b1 C4 Z6 Y9 qAndroid Pad Moto Xoom+ S" H6 t8 T8 d" w0 [
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
# t; ]. f, Y6 C4 N" K7 ^, o+ m) ?7 w  P6 e2 H- L3 C3 C
BlackBerry
/ V+ m8 z5 K8 WUser-Agent: Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+; k3 k( U  A2 W, x- N2 f
& ?# r$ V* m4 {/ l/ _4 d$ M5 A
WebOS HP Touchpad
2 p/ c) q2 f- m- M& _& W4 PUser-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.09 I4 }2 `4 @+ D7 A
  b( P* o$ a! D3 N2 B
Nokia N97
; o/ x5 E7 z9 j0 M  VUser-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
$ X7 f; a$ Q, R, @4 h. T7 n
  S# @1 c/ j# BWindows Phone Mango
, g, f' D: [+ Q; y- ?1 ]User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)( U# `; S3 y8 r
/ i6 k1 h9 A/ U2 ]  E! H6 a
UC标准6 ], G/ j9 E+ t/ @4 \  X: h6 C6 l
User-Agent: NOKIA5700/ UCWEB7.0.2.37/28/999
4 [/ Z" c+ a8 t' G5 ~( W& h" J' H: E. x1 K8 v* Z
UCOpenwave
+ n: A8 q) _% ]6 h0 }/ aUser-Agent: Openwave/ UCWEB7.0.2.37/28/9995 ~5 p8 E; P) w6 ]3 }1 ~6 p

/ p. |& K0 r  [$ \' @+ G, rUC Opera
7 h4 C& b, ?% E$ Y! [4 DUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/999

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

美国VPS、域名代购:http://tu8l.taobao.com

返回列表
Namecheap
Namecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting
互联网安全