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

; L/ w3 [& S% X7 T8 q


1 o! o! W& w7 v

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.         %>
复制代码
- M* O" @0 z# y8 Z5 X& u. `9 B

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.         }
复制代码
0 U; E, b6 T$ a/ F7 h% Z

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

6 e! b4 ~& J  b. l
/ A3 O+ `4 T8 a) r. n

浏览器UA大全

1、主要浏览器

safari 5.1 – MAC
$ O) S# l2 {; t8 tUser-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
) w* ^: G/ ^1 H: U7 m
. U5 B& J$ e& {/ e$ Ysafari 5.1 – Windows& h% L) D: A- o) |
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: E, g! b# |2 w6 ~: M1 F
8 X2 A* L& L7 _
IE 9.01 A" M; D) }  o( m, Z2 Z4 \
User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;3 n2 j6 n0 ?! ]4 V. S* N
. o. [; h# ]2 d
IE 8.0
/ d1 ^* q  R5 ~% Z, }0 kUser-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)
  V/ o# o( \/ z7 X: |2 X  o5 l( v% a( }
IE 7.0
. |) h4 ]  j# A+ eUser-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)+ s2 O! f, x$ [6 H7 X% L
1 B5 V! q! R# X5 _
IE 6.0
; H  o8 V  q! r# JUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
( z# `& O* e2 ^  E) Q3 m  ]! [& W2 l; \+ Z" i+ i, K' T9 |
Firefox 4.0.1 – MAC  Z6 U# M: p, |) f
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
% k6 ]* H, k2 Z6 E: x  [" }/ Y! b" o$ x# X4 x$ \# ^7 T1 s& ^
Firefox 4.0.1 – Windows# ]. O: Y2 y. F8 E2 n$ Q( [( F
User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1/ t8 h7 S1 C) G3 B
8 C! \" m# V$ T4 f2 N
Opera 11.11 – MAC  X4 |1 n* p1 r8 v
User-Agent:Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.118 J) K2 Q( U% e  w

$ [/ ]0 J( E5 tOpera 11.11 – Windows
4 D: C7 w. E8 f, v2 n6 u, PUser-Agent:Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11
- V/ M$ x* e' k7 U/ W# w
. \3 \$ i3 z2 w+ sChrome 17.0 – MAC" h/ {' q& {. z7 c/ R7 n4 p
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
/ j* Z' w, q' q4 s3 r# Z9 jUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)
9 x* g- Y, ~( j* Z% y' R" O/ O) I, I  p( V
TT+ X- [' t- t7 \+ [! a5 l- ]7 g
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)
" J5 A0 f% c2 {0 c5 A6 B: j$ v" L% d( [, T# Q9 N
The World 2.x
2 B6 t" N/ {& N" \/ d2 g  Y$ u4 rUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)
, v" d1 X" B; o( o5 k* d  y
0 D* H( ?) [. ]$ cThe World 3.x
% m/ t! r0 i8 h, }* o- c! {User-Agent:?Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)& K% ^* A; ^/ H" K# q6 k2 q
3 f5 O0 a* n/ e$ g6 t# T5 @2 p
搜狗浏览器 1.x' [3 X& |+ n6 J6 |" m8 z! N
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)
6 V: J& J1 f+ o9 I" d
1 `" _0 R# y0 a) T- R6 g360SE! v/ A2 Q, W$ u  P4 N8 L
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)0 f& X8 N( w3 Q2 T3 r8 J

0 N1 ~7 e. _% \Avant
# u0 T, ?$ T3 ~6 i0 U; u4 eUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)+ n8 e/ T6 J+ o: t' a; E! o

+ q+ ]+ `. y0 e0 m9 Z' h4 A' @Green Browser5 }; v, o* q" v2 R% i
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)

3、移动设备端

safari iOS 4.33 – iPhone$ n9 q: Q: C( l: F+ A
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
' B: w/ Y2 @' e9 @/ b) V5 }* G7 P' b/ F0 C1 r
safari iOS 4.33 – iPod Touch, h) U! A/ B  B. R% X* h+ q. t
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
. ]& p# h: Z& {- [
" c" B6 {" W8 `: Wsafari iOS 4.33 – iPad  C. W2 {) X) |: q$ W4 ^, D: H: j$ \
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.50 \( u: w8 O7 i! {" K
' n- L; K4 D1 O( A! X" k
Android N17 L: R; n7 d' r
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.15 n6 ~8 R% u4 J2 T) x7 {* ?# g
- n% D" B* S2 b4 @- \* a
Android QQ For android* m% w4 o/ T- u$ X1 M: N
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( m* j* V; i- n4 x# ^" J  R6 W7 O

- H! x# {, j( `Android Opera Mobile
$ u- M9 ]6 k1 d1 CUser-Agent: Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10
7 f& X2 W# s5 `( {4 M# m
5 c- N3 `' N. H" H5 r" B& dAndroid Pad Moto Xoom
# v2 I" R8 r. g& `% U' D7 }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' O9 ?  B( |& S9 Q5 y; l

3 h5 ~) i% u4 V) \BlackBerry& z% D8 F. h2 }! G9 M! \) F; h
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+
1 y( V8 ]6 g* ?, y  D9 H! b* q: Y- }5 Y# z# K
WebOS HP Touchpad
# i2 V: {5 T: U1 d$ EUser-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.06 R6 I8 ~" @1 [: ^$ _( Q5 D  R4 v) M

9 Q3 Z6 Z( j3 n: D  ~/ RNokia N97
  V. i% r# v3 i( _; xUser-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
; S! l2 y/ G8 _2 N% [  @1 J1 n
- D1 j% n8 N5 o$ ]4 i5 P/ K" d5 RWindows Phone Mango
& c. G" \4 ?( O$ \. F0 iUser-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)3 i  W" }/ W- |8 F6 L
! Q6 h: r; O2 s! g0 d
UC标准
! N! E2 S; }' {% K' H+ qUser-Agent: NOKIA5700/ UCWEB7.0.2.37/28/999; k4 o9 D3 o3 X, V' m! A

1 ~: r7 _# l! F# UUCOpenwave
$ Y! w3 B9 z/ W# ~. G' p' |3 L. OUser-Agent: Openwave/ UCWEB7.0.2.37/28/999
7 T% G. G/ z* A5 P( F& M4 O; I& D# |+ A
UC Opera
5 ], ]) M* U8 d8 k, \; _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