当前位置: 移动技术网 > IT编程>开发语言>Asp > Asp限制IP访问 阻止某一个IP段禁止访问本站的代码

Asp限制IP访问 阻止某一个IP段禁止访问本站的代码

2017年12月08日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

<%
''获取访问者的地址
ip=request.servervariables("remote_addr")
''允许的ip地址段为10.0.0.0~10.68.63.255
allowip1="10.0.0.0"
allowip2="10.68.10.71"
response.writecheckip(ip,allowip1,allowip2)
functioncheckip(ip,allowip1,allowip2)
dimcheck(4)
checkip=false
ipstr=split(ip,".")
allow1=split(allowip1,".")
allow2=split(allowip2,".")
ifcint(allow1(0))>cint(allow2(0))then''判断ip地址段是否合法
response.write"禁止访问"
exitfunction
endif
fori=0toubound(ipstr)
ifcint(allow1(i))<cint(allow2(i))then
ifcint(allow1(i))=cint(ipstr(i))then
check(i)=true
checkip=true
exitfor
else
ifcint(ipstr(i))<cint(allow2(i))then
check(i)=true
checkip=true
exitfor
else
ifcint(ipstr(i))>cint(allow2(i))then
check(i)=false
checkip=false
exitfor
else
check(i)=true
checkip=true
endif
endif
endif
else
ifcint(allow1(i))>cint(ipstr(i))orcint(allow1(i))<cint(ipstr(i))then
check(i)=false
checkip=false
ifi<>ubound(ipstr)then
exitfor
endif
else
check(i)=true
endif
endif
next
if(check(0)=trueandcheck(1)=trueandcheck(2)=trueandcheck(3)=false)and(cint(allow2(2))>cint(ipstr(2)))then
checkip=true
endif
endfunction
%>

把以下代码加入到你的asp页面就可以测试到效果了:
复制代码 代码如下:

<%
'受屏蔽ip地址(段)集合,星号为通配符,通常保存于配置文件中。
const badipgroup = "192.168.1.*|202.68.*.*|*.12.55.34|185.*.96.24|127.*.0.1|192.168.0.1"
if isforbidip(badipgroup) = true then
response.write(getip &"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
''返回客户ip地址
function getip()
dim ip
ip = request.servervariables("http_x_forwarded_for")
if ip = "" then ip = request.servervariables("remote_addr")
getip = ip
end function
%>

  这样就可以限制网站访问的ip段了,你可以根据地区的ip段来设置。
  我就是用这个方法防止外地用户使用流氓软件恶意到我网站发布信息的!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网