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

(编辑:jimmy 日期: 2024/9/24 浏览:2)

复制代码 代码如下:
<%
''获取访问者的地址
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段来设置。
  我就是用这个方法防止外地用户使用流氓软件恶意到我网站发布信息的!
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?