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

( J& u3 a# R% W0 @8 |% r% e


" j. R5 f! y/ c- m

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

$ ~6 d: ~8 \: 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.         }
复制代码

; l' `, X$ o; C+ a

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

( W$ b3 S: P7 J8 H% T
8 [1 ]1 h& Y+ \

浏览器UA大全

1、主要浏览器

safari 5.1 – MAC
9 r) t* _4 T' i- |6 o( C0 h8 b$ fUser-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" l& I% ~4 y6 i5 S6 S) Y

5 |# ]: v1 h7 h) m; Q% ]safari 5.1 – Windows* n' o* J8 W! {: q+ D1 }7 ]7 f* U, b
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
- }& N& s7 `2 o5 W' ^7 k" w& O7 w
IE 9.05 _0 j8 g' D2 g6 ~
User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;
- x6 ]8 U: [/ v5 K( t
/ f* f3 d8 d1 yIE 8.0
" }5 V5 r+ e% T# Z' g3 g" YUser-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)
5 w8 e" n7 V7 z0 m" ~1 k. C. M/ P' l; J9 y6 v* c3 H5 \
IE 7.09 I' a+ l' P! V. Q1 g: Z" b
User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)
* l" [5 B0 y* v5 s, R( `+ w! g; x# q
IE 6.0: R; N, ^! \& U9 I4 F& \
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
; X$ Z) m1 G! F! f0 I! V3 _9 Q/ Q0 l7 Z& G0 t. l$ R: o' l$ E3 X
Firefox 4.0.1 – MAC
" v- ^/ V% i/ |0 O. o& F, l& cUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1' A  t' {7 u* t9 ~6 a5 f

9 L; }) [6 r/ {* u! `Firefox 4.0.1 – Windows9 R% H# [, m' @3 m
User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.15 {$ ~, A# v; y2 Y# R

9 T  k4 a$ f3 R7 DOpera 11.11 – MAC/ |% u; l$ J3 U4 o+ m; F
User-Agent:Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11
. f  u& s& d% y
& s3 j' S( k4 ]! x) d' VOpera 11.11 – Windows# z2 E) k* K; J5 P2 Q: k
User-Agent:Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11; j; V2 w2 |4 B, c

0 S" n, u) I& \' s  BChrome 17.0 – MAC" q& D& M( y, H/ ^/ e( \
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! ]& O" m- ^1 j1 V/ t) i
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)
6 C/ K! m1 I2 h2 S5 T. {+ x* D
. u, B4 V8 ]/ S/ [4 `TT6 L" D5 }% I& W
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)$ J/ l: t( i! H# z2 w

+ `( g3 e8 |2 p" ]  _# kThe World 2.x% C  G6 H) Z1 K, e) ?
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)' @! |: `. [, Q8 D
: D# M1 r- E& {3 r6 ]. a% ~& ]% B
The World 3.x
1 H6 m6 |0 g* v: oUser-Agent:?Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World), k! Y% j+ {/ g. x
2 B  ~/ e7 X' _( J2 t
搜狗浏览器 1.x
+ g2 t" @$ F8 p: V' x, @- r% QUser-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): S6 Y: m8 k" j
$ u/ Y7 w2 \* U$ s
360SE
) V8 D* j4 p2 h* a/ UUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)/ ]5 y; q- `2 K( [) T; E

! y/ q1 c. J! r$ ?6 L6 G" |5 BAvant$ X) M0 ~2 y4 t
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)" J) a4 w- h; U9 T- V
6 R# }0 r* \) Q
Green Browser
8 n5 d6 D) }) |& qUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)

3、移动设备端

safari iOS 4.33 – iPhone2 T( g% g* N8 f+ ^1 p# U
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* H: E9 n+ C- Z/ b2 v4 G

& r' j2 Z: A# \. usafari iOS 4.33 – iPod Touch
% Q2 b: ]! @* I' f8 P% N" EUser-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
5 N  v3 l6 E3 C5 Y! z/ ^: v9 J8 R: u) A/ l1 L+ y
safari iOS 4.33 – iPad0 x+ O9 Z) y1 p! l4 ^
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, @7 F% `& v6 f0 r' |) X: Q3 I
2 s, |4 K1 M# y1 `! ], g
Android N19 T7 A& c9 I3 q9 \
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.13 w) W4 j! n+ ?) T9 f; q+ \: R' Q# m

- k( f; m. ]- o1 q  u7 HAndroid QQ For android
* Q. J# @  ~5 K7 W2 DUser-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.10 C, _, F- g/ x
' o* ~; F; B: [4 Z* A. C
Android Opera Mobile3 q7 ^! q4 Y) G" s0 O# T
User-Agent: Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10' O, H) x# O7 T1 d, }! c5 J! F! a

% ^8 |9 l/ N. q. W# G% uAndroid Pad Moto Xoom
# f/ g2 E' e- r/ EUser-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
2 U1 i) B0 z5 J' e
$ X& R- ~0 z! [0 M, V7 `BlackBerry
$ y8 a/ f. X1 m( |( BUser-Agent: Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+
  {* P) d, O9 P) q: o' s9 s
. m% U! d8 R$ R: w& e. d1 @4 YWebOS HP Touchpad5 h2 s' Y* f; z1 {# g! h0 ^
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
( Y$ ]+ D, g  i3 q- y) {/ F
) ]/ @, p& _4 i. h, t! JNokia N97# K$ y8 g/ c4 e4 u; b4 y- Q5 L
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
) T! X8 ^; t1 M; q5 D6 Y, r. P, z, Z0 r7 |. @" X0 B
Windows Phone Mango' |& M2 }/ x3 @) e7 z9 j' y
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)
3 f3 O/ e/ `  S+ c# x2 j
) d5 V" ]% S" b  i( dUC标准" P8 N8 }6 H# A7 W7 m
User-Agent: NOKIA5700/ UCWEB7.0.2.37/28/999
. c, Y/ {  b0 A- P. T9 M) t& k, _' g/ o* g! P
UCOpenwave
6 B1 }3 G& _/ ^$ \; JUser-Agent: Openwave/ UCWEB7.0.2.37/28/999. \& J* @& n! Y2 n: G& x* [
# @" c, |6 P- M+ |
UC Opera
! S: c# P5 }1 Q3 }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