1. 程式人生 > >ASP 中如何判斷PC使用者訪問還是手機使用者訪問

ASP 中如何判斷PC使用者訪問還是手機使用者訪問

<%
'判斷是否是手機上網
'response.write Request.ServerVariables("HTTP_ACCEPT")&"<BR>"
'response.write Request.ServerVariables("HTTP_USER_AGENT")&"<BR>"

URL="http://www.iscripts.org/?mobile=yes"
If Request.ServerVariables("HTTP_X_WAP_PROFILE") Then '如果有HTTP_X_WAP_PROFILE則是手機
' response.write "手機使用者1"
response.redirect URL
response.End
End If


'如果只支援wml並且不支援html則是手機
If InStr(LCase(Request.ServerVariables("HTTP_ACCEPT")),"vnd.wap.wml") > 0 And InStr(LCase(Request.ServerVariables("HTTP_ACCEPT")),"text/html")=0 Then
' Response.write "手機使用者2"
response.redirect URL
response.End
End If


'如果支援wml和html但是wml在html之前則是移動裝置
If InStr(LCase(Request.ServerVariables("HTTP_ACCEPT")),"vnd.wap.wml") And InStr(LCase(Request.ServerVariables("HTTP_ACCEPT")),"text/html") Then
If InStr(LCase(Request.ServerVariables("HTTP_ACCEPT")),"vnd.wap.wml")< InStr(LCase(Request.ServerVariables("HTTP_ACCEPT")),"text/html") Then
' Response.write "手機使用者3"
response.redirect URL
respone.End
End If
End If

'如果手機User_Agent有品牌特徵符則是手機
Set reg = New RegExp
str="CECT,Compal,CTL,LG,NEC,TCL,Alcatel,Ericsson,BIRD,DAXIAN,DBTEL,Eastcom,PANTECH,Dopod,PHILIPS,HAIER,KONKA,KEJIAN,LENOVO,BenQ,MOT,Soutec,Nokia,SAGEM,SGH,SED,Capitel,Panasonic,SonyEricsson,SIE,SHARP,Amoi,PANDA,ZTE"
str=Replace(str,",",")|(")
reg.pattern=".*("&str&").*"
reg.IgnoreCase = True
response.write str&"<BR>"&reg.pattern
If reg.test(Request.ServerVariables("HTTP_USER_AGENT")) Then
' response.write "手機使用者4"
response.redirect URL
respone.End
End If
%>

第二種,這個效果會好一些

HTTP_ACCEPT=Request.ServerVariables("HTTP_ACCEPT")                 '獲取瀏覽器資訊
HTTP_USER_AGENT=LCase(Request.ServerVariables("HTTP_USER_AGENT"))  '獲取AGENT
HTTP_X_WAP_PROFILE=Request.ServerVariables("HTTP_X_WAP_PROFILE")   'WAP特定資訊 品牌機自帶瀏覽器都會有
HTTP_UA_OS=Request.ServerVariables("HTTP_UA_OS")                   '手機系統 電腦為空
HTTP_VIA=LCase(Request.ServerVariables("HTTP_VIA"))                '閘道器資訊
Dim WapStr
WAPstr=False
If ubound(split(HTTP_ACCEPT,"vnd.wap"))>0 Then WAPstr=True
If HTTP_USER_AGENT="" Then  WAPstr=True
If HTTP_X_WAP_PROFILE<>"" Then  WAPstr=True
If HTTP_UA_OS<>"" Then  WAPstr=True
IF ubound(split(HTTP_VIA,"wap"))>0 Then  WAPstr=True
IF ubound(split(HTTP_USER_AGENT,"netfront"))>0 Then  WAPstr=True
IF ubound(split(HTTP_USER_AGENT,"iphone"))>0 Then  WAPstr=True
IF ubound(split(HTTP_USER_AGENT,"opera mini"))>0 Then  WAPstr=True
IF ubound(split(HTTP_USER_AGENT,"ucweb"))>0 Then  WAPstr=True
IF ubound(split(HTTP_USER_AGENT,"windows ce"))>0 Then  WAPstr=True
IF ubound(split(HTTP_USER_AGENT,"symbianos"))>0 Then  WAPstr=True
IF ubound(split(HTTP_USER_AGENT,"java"))>0 Then  WAPstr=True
IF ubound(split(HTTP_USER_AGENT,"android"))>0 Then  WAPstr=True
If WAPstr=True Then    
Response.Write "請訪問手機版本http://m.logo880.cn"   
response.redirect "http://m.logo880.cn"
response.end
else   
'response.redirect "http://www.logo880.cn"
'response.end
End if