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.         ?>
复制代码
1 [7 N8 ^4 P  _: ?0 f


6 O% r% j3 F! v7 b9 _" F

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.         %>
复制代码
+ f1 U# `7 b- A: w2 c

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.         }
复制代码
/ r2 u+ \1 r, X- M/ k- ]

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


3 a/ T7 \4 t( z( e" O/ D7 i$ {- `8 m2 [$ f6 O" y% Q* G

浏览器UA大全

1、主要浏览器

safari 5.1 – MAC# g( D# e$ T; i
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& ~& h( N( v" Q# m, W# I  {- n
3 i+ m. i- E* |6 ]9 x+ R  b
safari 5.1 – Windows
+ e$ Y6 r3 n( m3 Q9 aUser-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50
. V" e) g+ t! c1 y/ a0 \5 X2 t$ \& Z- a: x/ J
IE 9.0
" D- {0 X1 z% D# L& dUser-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;
5 B9 T4 _2 l7 K, v
6 l4 m  f% ]3 V* q& e7 e8 QIE 8.0
) h/ \3 l- ]/ I) DUser-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)3 f# `+ ^4 P% m7 S4 N4 O

9 `8 a$ l' g: F/ u9 M  xIE 7.0/ p) b1 b. k2 p* u, W& q
User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)
) H" d2 @, N8 @) k9 }. W! j
% X# Q- t+ r# r  Z  F6 D! XIE 6.0
9 m$ L. Y* [) {$ Q" y1 C' ]: lUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1): B' F: @+ B. S& n% M

2 x( q# G* ^3 _6 k) s: q6 ^Firefox 4.0.1 – MAC
9 t9 \, i( M  D" k# [3 gUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1& F" i$ W, ]1 k2 Z& t, K
; Z' C* y+ y! s! E: i* J, B, h# j
Firefox 4.0.1 – Windows
  K4 b+ w! d# ^% d3 CUser-Agent:Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1# o3 |  _+ ^. }: L: `

6 z% o5 ~3 o) _2 B: E/ d" VOpera 11.11 – MAC: b# A0 q' m# Y4 i$ C* T# s5 H  q
User-Agent:Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11. e; p' r' O! D5 T/ Y- @
$ s2 R. d0 f% n; _% ~8 `. b5 z1 N
Opera 11.11 – Windows" Q. C  {. m1 z: X" Z5 c% W2 Q& P
User-Agent:Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11
  r* |1 y, t, G8 x% L0 f% s& Y2 r$ g, m  I6 i0 h
Chrome 17.0 – MAC
6 Y, o* M( Y  r; O: iUser-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" j3 t5 }# h  _) Q, `" L
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)) d# v6 F2 Z% ]4 J* _) c2 N* a

* v# {) s5 K) k; UTT
7 _  ~# h+ E7 z4 d) aUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)
) \" u! Q4 B, t& S- Y: I( c( d: O! m; K& S
The World 2.x
# J# H0 V1 x+ \+ \& v# o( cUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)3 c% E4 |& h& C. \

0 ]+ C0 w9 W6 T5 U1 b+ s: GThe World 3.x
, o; K7 W; n5 K1 e1 S" w, }User-Agent:?Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)
2 \& Y( R9 T! r
' B' b- h3 Q& }8 o! {- c5 K搜狗浏览器 1.x5 |, _. w! l# A" g% H4 ]
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). b$ Y  m' Z2 X: s( G+ {

# {1 d2 }1 p6 Y: n3 a360SE# O. B( m. m* t
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)
2 ]# U) U# ^: a  P4 r
/ s7 B8 Y! O! Q, Y. J+ U# c( oAvant  j" r+ f+ }7 `/ G! Q/ f
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)
% r) `  Y& @) l3 o+ ^) f* F: z8 c# S  L/ H
Green Browser
# c9 L# q  s5 s0 ~- M. y* G8 pUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)

3、移动设备端

safari iOS 4.33 – iPhone
  F5 ^, e* \& T& K; R/ @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( S; k$ m! a# k3 ^1 i  E% P
8 ?9 I% v% q1 R
safari iOS 4.33 – iPod Touch+ v# j, ?4 r. O7 {& {
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
6 L4 A$ S" G$ R) `) ?/ P; H% ^
$ c+ J; M+ L* o4 Qsafari iOS 4.33 – iPad6 }- u" `+ `( k. E- }
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
% ?% n' q& w2 K' Q9 @. [1 ?  r- q. V* X" @: ?. r; v$ F
Android N1
! V8 c# |! k9 S2 D5 L9 T1 u0 K# pUser-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
! t1 y1 Y' ~- [  x4 U
  C9 [6 B. |) c- C& h& J. iAndroid QQ For android
* r) {9 c7 H7 U% H! C; m3 G- pUser-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
# w: l& M' N- N+ R" N7 P8 E) L' }) L) ^6 B: v' F) Y
Android Opera Mobile$ D, h2 _7 ]' \, |
User-Agent: Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10
# @, E1 e* y# {' C  ^: {
, m1 X. ^. H- _+ lAndroid Pad Moto Xoom& E+ c  ~3 o% w8 n
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+ ]& [) R& ^1 R( D, B
5 R) W2 L& |& C! j/ W/ Q2 l) R$ P
BlackBerry7 J( D3 a& r0 o( U7 A5 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+
, ]- ?1 t: I8 K+ K5 z( T( `, Z" o: _
WebOS HP Touchpad
3 H) q2 B% h0 h( t& KUser-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
, `; X! `) {9 k
9 A6 ~6 {; {* e( n: I3 CNokia N97* R& f1 w- E* v5 ]
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! {* j4 r, L/ ?' c$ ^0 t' u
7 r7 w5 I4 I, I9 _& n+ }, _& I/ x
Windows Phone Mango6 y2 i' B9 B$ h9 S7 p/ V+ Z
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)
9 K. g+ p$ p' I& r1 s  Y) ?
' W$ O: }+ l. OUC标准. J) C7 _" j7 W$ F4 T
User-Agent: NOKIA5700/ UCWEB7.0.2.37/28/999
7 `3 P5 t# S3 n7 z4 M2 C2 k( ^% l' l+ X" D3 h. _, \+ o, |
UCOpenwave
. z1 y$ R1 n& r% b+ s4 XUser-Agent: Openwave/ UCWEB7.0.2.37/28/999' ], p# T' U3 _

8 a  z4 e* Q6 G8 j/ ^& r. ], m/ m4 AUC Opera  G. L- v- i0 Y# g' d2 y
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