<%
'asp取得用戶的真實(shí)IP
'以下是網(wǎng)上找到的說明
'1. Request.ServerVariables("HTTP_CLIENT_IP") 有,只是未成標(biāo)準(zhǔn),不一定服務(wù)器都實(shí)現(xiàn)了。
'2. Request.ServerVariables("HTTP_X_FORWARDED_FOR") 是有標(biāo)準(zhǔn)定義,用來識(shí)別經(jīng)過HTTP代理后的客戶端IP地址,格式:clientip,proxy1,proxy2
'3. Request.ServerVariables("REMOTE_ADDR") 是可靠的, 它是最后一個(gè)跟你的服務(wù)器握手的IP,可能是用戶的代理服務(wù)器,也可能是自己的反向代理
function checkip(checkstring) '用正則判斷IP是否合法
dim re1
set re1=new RegExp
re1.pattern="^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"
re1.global=false
re1.Ignorecase=false
checkip=re1.test(checkstring)
set re1=nothing
end function
function get_cli_ip() '取真實(shí)IP函數(shù),先 HTTP_CLIENT_IP 再 HTTP_X_FORWARDED_FOR 再 REMOTE_ADDR
dim client_ip
if checkip(Request.ServerVariables("HTTP_CLIENT_IP"))=true then
get_cli_ip = checkip(Request.ServerVariables("HTTP_CLIENT_IP"))
else
MyArray = split(Request.ServerVariables("HTTP_X_FORWARDED_FOR"),",")
if ubound(MyArray)>=0 then
client_ip = trim(MyArray(0))
if checkip(client_ip)=true then
get_cli_ip = client_ip
exit function
end if
end if
get_cli_ip = Request.ServerVariables("REMOTE_ADDR")
end if
end function
response.write get_cli_ip
'本地測(cè)試顯示結(jié)果:127.0.0.1
%>