用asp实现:限制北京地区的Ip访问网站
用asp实现:限制北京地区的Ip访问网站,请教怎么实现呀?
[解决办法]
网上找北京ip段数据库,然后获取客户端ip地址和数据库对比,属于北京ip段的就继续执行代码,否则response.end结束
[解决办法]
<%
'受屏蔽IP地址(段)集合,星号为通配符,通常保存于配置文件中。
Const BadIPGroup = "北京字段"
<!--例如这种:Const BadIPGroup = "222.168.27.*
[解决办法]
218.62.26.*" -->
If IsForbidIP(BadIPGroup) = True Then
response.write "你的IP地址被禁止访问本站"
Response.End()
End If
'参数vBadIP:要屏蔽的IP段,IP地址集合,用
[解决办法]
符号分隔多个IP地址(段)
'返回Bool:True用户IP在被屏蔽范围,False 反之
Function IsForbidIP(vBadIP)
Dim counter, arrIPPart, arrBadIP, arrBadIPPart, i, j
arrBadIP = Split(vBadIP, "
[解决办法]
")
arrIPPart = Split(GetIP(), ".")
For i = 0 To UBound(arrBadIP)
counter = 0
arrBadIPPart = Split(arrBadIP(i), ".")
For j = 0 To UBound(arrIPPart)
If(arrBadIPPart(j)) = "*" or Cstr(arrIPPart(j)) = Cstr(arrBadIPPart(j)) Then
counter = counter + 1
End If
Next
If counter = 4 Then
IsForbidIP = True
Exit Function
End If
Next
IsForbidIP = False
End Function
Function GetIP()
Dim IP
IP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If IP = "" Then IP = Request.ServerVariables("REMOTE_ADDR")
GetIP = IP
End Function
%>