$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.         ?>
复制代码
1 P' i+ C/ E6 f! t( ~0 L


4 X2 c. ~# L  w9 ~. [

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.         %>
复制代码
# z  _" W2 d. f! ?, X) g" 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.         }
复制代码

! o7 a5 p* m* Q

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

& w6 A+ |1 i/ E

! T/ a# I8 m/ V( [0 u( m4 c

浏览器UA大全

1、主要浏览器

safari 5.1 – MAC" Z0 \$ N2 b/ C4 O0 a
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.503 n! l5 g" w5 S5 i* Z  T7 R# ?3 a( G

  U7 o/ |5 `3 E! fsafari 5.1 – Windows
/ @  X1 M! \: E, g1 d- t; u. bUser-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50. P) \/ n( N+ t. F

% B  j# d+ a+ U+ ]. ~/ x7 o! `IE 9.0# \6 U( g3 ]3 G! r
User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;1 d7 m: F1 U+ r  L  E2 @& v
: \* |( d& v& {  Q  W
IE 8.0
/ K8 W+ L; [: U/ j- |User-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)
+ |- v! d+ U$ D* i$ ?! B$ V5 q9 p* |/ H$ f9 R2 R. b* F- {
IE 7.0
$ ?$ T3 i# m: oUser-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)4 X/ _, g* @+ f2 f; U. b
- W2 v9 {: x+ K
IE 6.0
) E: ]8 u: e3 G. ]. c7 _User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)& W% I/ @- p9 C$ n# K, a
5 l0 ^0 V& m* r# i
Firefox 4.0.1 – MAC
) L4 D6 f- }. H/ ~6 `. UUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1* W0 B3 K9 m& ~6 c, b: i! M- b
! ^$ U. Q+ Y. I- Z# i1 w) Z0 I
Firefox 4.0.1 – Windows$ L# y* _% P* H4 p' r$ P7 W
User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
, B$ C8 K/ q5 @% K( e. h
, Y0 {  [$ s  @Opera 11.11 – MAC
* f0 t. E4 s+ C, j' c$ cUser-Agent:Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11
& h2 p$ o$ W5 B$ E  J" v  h" S4 A1 g+ W! z4 c0 u
Opera 11.11 – Windows
7 f/ V9 N& b7 h+ N1 TUser-Agent:Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11
% [' H5 t0 e/ a- i( G: k3 j& ^3 Q0 Q. q6 a! @
Chrome 17.0 – MAC
1 i5 e, n2 C8 `: T) \5 l7 qUser-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* T( G/ b6 R; `/ @& L) N+ O) z
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)
3 q& h3 m: I9 c- s$ @4 G' e2 T
6 t, j5 f+ b3 L* WTT
3 {$ P2 x" N! d, u* s% jUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)
; A; s, ^, M# l3 x  C. R) Y: ]9 _; @8 n: T3 n; Z* I
The World 2.x0 n( c1 r9 Y: `1 {  R
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)5 ]* A% R; W3 ?" J* N

* P. y( N: H9 o: rThe World 3.x6 ^9 |' a; E: ^
User-Agent:?Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)
$ k: G$ g/ S0 r1 ?6 L
3 B7 Y7 z9 L2 q1 _搜狗浏览器 1.x7 y. {5 B4 ?% X5 S3 a6 e
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)5 C6 @: k/ L3 m8 q( K3 l- `5 o
8 r7 J- O3 |+ M  A/ G$ w; v6 i! T
360SE$ V) _( R$ p/ L
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)* ~& }* R3 v; W4 c1 C( L
" W. [" ]  c( N& g
Avant
: x' Y2 y% m# ~- `/ n+ e2 g0 @; _3 CUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)# w8 m7 \0 I4 x: ?5 j

& m& Q  D8 Y5 ~* rGreen Browser. Y2 t  B( C/ q$ m7 Q; e
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)

3、移动设备端

safari iOS 4.33 – iPhone; u! D0 x# S& d$ k
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
  n4 a+ l0 o  U$ ?7 x: k; l
" {- w  `5 i5 p7 P, gsafari iOS 4.33 – iPod Touch
  L  W3 ^& y: n' X, gUser-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.53 O/ c" N7 E# L# t
% {  [( O  w3 I& u: H( ?
safari iOS 4.33 – iPad! E1 n, ]7 A8 R
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.55 {3 p7 y& v8 M& P  S, Y/ [

8 R+ g8 u; y& j# X2 r/ m6 cAndroid N1, O- `9 p+ e: T
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
5 T5 Z& W( v' X7 h4 I' T" Q! L1 T$ c) p4 E" N& \3 V
Android QQ For android! @9 x: W0 q# @' |
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
0 W- {7 h- A) ~
! e' I9 D) ]5 T% |: ]Android Opera Mobile% b4 }/ Y- Y5 r! K- G
User-Agent: Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10
: X, n0 j7 `# s; U
; o( }# y0 u% M8 @Android Pad Moto Xoom
& l, \7 M' G$ }/ N5 y% u+ fUser-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
0 @# A% I2 H/ N4 ?; H+ e
& g" u. n$ ~/ o; F$ G1 I3 L! ]BlackBerry
) o8 H; B, Q5 @9 \9 FUser-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 S4 z$ j7 C/ ~6 Z1 A3 C& {+ l$ l. i' R. T. W
WebOS HP Touchpad
9 w2 T. U& x/ F9 O* XUser-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.04 Y9 w( |# G0 F
* k" f% W, t& W5 G: e0 ?. W& i7 ?
Nokia N97
4 R/ M& s' z/ BUser-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.181248 V, l! J1 P" L, X) v+ i

" P2 P! h3 j0 P0 {0 c4 `Windows Phone Mango9 r# T( Y8 ~7 C: y4 K# Z
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)
  ^+ d, V9 R0 a! X
. i" x3 m+ q9 T7 y) Y: |UC标准- _# q- o; f0 V* z+ C% W5 d
User-Agent: NOKIA5700/ UCWEB7.0.2.37/28/999
: b: l  l2 `; @7 ^3 |* `- q
3 i3 O% p- u8 y+ bUCOpenwave$ j% [/ V4 f. ?0 W9 O- G
User-Agent: Openwave/ UCWEB7.0.2.37/28/999
$ G3 B  F+ b' @1 X7 q6 k; i2 q9 d8 ^3 p4 ~- G$ k( p! d6 j
UC Opera% U$ M  h$ U6 s: i+ |" k* A' S
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/999

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

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