快速取得目前頁面完整 URL、客戶端 IP、伺服器資訊、HTTP 標頭等關鍵環境變數,附常用鍵值中文對照表與組合範例。
' 方案一:標準組合 (含 QueryString)
fullUrl = "https://" & Request.ServerVariables("HTTP_HOST") & _
Request.ServerVariables("URL") & _
"?" & Request.ServerVariables("QUERY_STRING")
' 方案二:Script_Name 替代 URL (含虛擬目錄)
fullUrl = "https://" & Request.ServerVariables("HTTP_HOST") & _
Request.ServerVariables("SCRIPT_NAME") & _
"?" & Request.ServerVariables("QUERY_STRING")
' 方案三:僅路徑 (無查詢字串)
pageUrl = "https://" & Request.ServerVariables("HTTP_HOST") & _
Request.ServerVariables("SCRIPT_NAME")
| 鍵名 | 中文說明 | 範例值 |
|---|---|---|
| HTTP_HOST | Host 標頭 (含埠號) | www.example.com:8080 |
| SERVER_NAME | 伺服器主機名稱 | WEB-SERVER-01 |
| SERVER_PORT | 伺服器監聽埠 | 80 |
| LOCAL_ADDR | 伺服器本機 IP | 192.168.1.10 |
| URL / SCRIPT_NAME | 請求虛擬路徑 | /folder/page.asp |
| QUERY_STRING | 查詢字串 (不含 ?) | id=123&cat=books |
| REQUEST_METHOD | HTTP 方法 | GET / POST |
| REMOTE_ADDR | 客戶端 IP | 203.0.113.45 |
| HTTP_USER_AGENT | 瀏覽器識別字串 | Mozilla/5.0 (Windows NT 10.0...) |
| HTTP_REFERER | 來源頁面 URL | https://google.com/search?q=asp |
| HTTP_COOKIE | Cookie 標頭原文 | sessionId=abc123; lang=zh-TW |
| CONTENT_TYPE | 請求內容型別 | application/x-www-form-urlencoded |
| CONTENT_LENGTH | 請求內容長度 (位元組) | 1024 |
| HTTPS | 是否 HTTPS | on / off |
| SERVER_SOFTWARE | 伺服器軟體版本 | Microsoft-IIS/10.0 |
If Request.ServerVariables("HTTPS") = "on" Then
' 安全連線
End If
Function GetClientIP()
Dim ip
ip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If ip = "" Then ip = Request.ServerVariables("REMOTE_ADDR")
GetClientIP = ip
End Function
Dim referer, host
referer = Request.ServerVariables("HTTP_REFERER")
host = Request.ServerVariables("HTTP_HOST")
If referer = "" Or InStr(referer, host) = 0 Then
Response.Status = "403 Forbidden"
Response.End
End If
logLine = Now() & "|" & Request.ServerVariables("REMOTE_ADDR") & "|" & _
Request.ServerVariables("REQUEST_METHOD") & "|" & _
Request.ServerVariables("SCRIPT_NAME") & "|" & _
Request.ServerVariables("QUERY_STRING") & "|" & _
Request.ServerVariables("HTTP_USER_AGENT")
HTTP_REFERER 可被用戶端偽造或瀏覽器隱私模式隱藏,不可作為安全依據REMOTE_ADDR 在負載平衡/反向 Proxy 後為 Proxy IP,需讀 HTTP_X_FORWARDED_FOR