[转载]使用ASP网页获得客户端的IP地址
文章作者:Siqi Wu我们可以使用asp通过Request.ServerVariables("REMOTE_ADDR")来获取客户端的IP地址,假设这是一张asp网页,我们可以通过以下代码来实现:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>这是一个获取IP地址的实例</title>
</head>
<body>
……………………
……………………
……………………
<% Ip_address=Request.ServerVariables ("REMOTE_ADDR")
Response IP_address
%>
……………………
……………………
……………………
</body>
</html> ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
获取 使用代理的客户的真实ip,客户未使用代理时这个值为空
ip=Request.ServerVariables("REMOTE_ADDR")
获取 未使用代理的客户的真实ip
if Request.ServerVariables("HTTP_X_FORWARDED_FOR")
=empty then
remoteaddr=Request.ServerVariables("REMOTE_ADDR")
else
remoteaddr=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
end if
response.write remoteaddr
读取用户地址
如果用了代理,则读出用户的实际地址 下面是一个限制用的程序,限制允许的地区访问,不允许使用代理服务器访问:
<%
IP = Request.ServerVariables("REMOTE_ADDR")
Function cacuIp(ip)
On Error Resume Next
Dim srIp, aIp
srIp=0
aIp = Split(ip,".")
If UBound(aIP)<>3 Then
cacuIP=0
Exit Function
End If
For i=0 To 3
srIp=srIp+(CInt(aIP(i))*(256^(3-i)))
Next
cacuIp=srIp-1
If Err Then cacuIp=0
End Function
Set CONN=Server.CreateObject("ADODB.Connection")
CONN.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ="&Server.Mappath("ip.mdb")
iIp=cacuIp(IP)
SQL = "SELECT country,city FROM IP_SoftYes WHERE ip1<=" & iIp & " AND ip2>=" & iIp
Set rs = CONN.Execute ( SQL )
If rs.Eof Then
sPlace="查无记录"
Else
sPlace=rs(0)&" "&rs(1)
End If
Set rs=Nothing
If not((InStr(sPlace,"北京")<>0) or (InStr(sPlace,"上海")<>0)) Then
Response.Write "错误的物理地址,你将不能访问。"
Response.End
End If
Set CONN=Nothing
If Request.ServerVariables("HTTP_X_FORWARDED_FOR")<>"" Then
Response.Write "本页不允许使用代理服务器访问。"
Response.End
End If
%>
另外需要一个IP数据库在同目录下支持。这个程序可以直接被include到其他程序内被使用。
页:
[1]
