一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

详细ASP获取用户真实IP代码

时间:2010-11-03 编辑:简简单单 来源:一聚教程网

正常情况这样就能取得客户端的ip地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的 ip 地址,而不是真正的客户端 ip 地址,要想透过代理服务器取得客户端的真实  ip 地址,就要使用微软公司在一般asp教程技术文档中并未公布的request.servervariables("http_x_forwarded_for") 来读取,但是需要注意的是:如果客户端没有通过代理服务器来访问,那么用 request.servervariables ("http_x_forwarded_for") 取到的值将是空的。因此,如果要在程序中使用此方法,可以这样处理:



欢迎访问本<a target="_blank" href="http://www.111com.net">网站</a>

<% ip_address=request.servervariables ("http_x_forwarded_for")
     if ip_address=""      then
    ip_address= request.servervariables ("remote_addr")


即:如果客户端通过代理服务器,则取 http_x_forwarded_for 的值,如果没通过代理服务器,就取 remote_addr 的值。

下面看一款真实实例

function get_cli_ip()'取真实ip函数,先 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

热门栏目