透過 Request.ServerVariables("HTTP_USER_AGENT") 取得 UA 字串,本函數 GetOs(content, type) 支援兩種模式:type=0 解析作業系統,type=1 解析瀏覽器版本。涵蓋 Windows 9x 到 Vista、Unix/Linux/Mac 等主流平台。
Function GetOs(content, systemOrBrowser)
Dim temp
temp = Split(content, ";")
Select Case systemOrBrowser
Case 0 ' 作業系統
If InStr(content, "Windows NT 6.0") Then GetOs = "Windows Vista"
ElseIf InStr(content, "Windows NT 5.2") Then GetOs = "Windows 2003"
ElseIf InStr(content, "Windows NT 5.1") Then GetOs = "Windows XP"
ElseIf InStr(content, "Windows NT 5.0") Then GetOs = "Windows 2000"
ElseIf InStr(content, "Windows NT") Then GetOs = "Windows NT"
ElseIf InStr(content, "Windows 9") Then GetOs = "Windows 9x"
ElseIf InStr(content, "unix") Then GetOs = "Unix"
ElseIf InStr(content, "linux") Then GetOs = "Linux"
ElseIf InStr(content, "SunOS") Then GetOs = "SunOS"
ElseIf InStr(content, "BSD") Then GetOs = "BSD"
ElseIf InStr(content, "Mac") Then GetOs = "Mac"
Else GetOs = "Other"
End If
Case 1 ' 瀏覽器
If InStr(content, "Opera") Then
GetOs = Replace(Left(content, InStr(content, "(") - 1), "/", " ")
ElseIf InStr(content, "MSIE") > 0 Then
GetOs = Trim(temp(1)) ' 通常第 2 段為版本
Else
' Firefox/Chrome/Safari 等
GetOs = Replace(Right(temp(4), Len(temp(4)) - InStrRev(temp(4), " ")), "/", " ")
End If
End Select
End Function
Dim ua, osName, browserName
ua = Request.ServerVariables("HTTP_USER_AGENT")
osName = GetOs(ua, 0)
browserName = GetOs(ua, 1)
Response.Write "OS: " & osName & "
Browser: " & browserName
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1UA 字串極易偽造且格式多變,建議:
UAParser.js 移植版或伺服器端 user-agent-parser NuGet 套件)。