-冰凌_ 发表于 2012-6-21 13:40:35

通过ASP禁止指定某IP段访问或只允许指定IP段访问网站的代码

本文转自情'blog

通过ASP禁止指定IP和只允许指定IP访问网站的代码,需要的朋友可以参考下。
一、禁止指定IP防问网站,并执行相应操作:

ASP代码
<%
Dim IP,IPString,VisitIP
'设置IP地址,用“|”隔开
IPString="|192.168.0.42|192.168.0.43|"
'获取IP地址
IP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If IP = "" Then
IP = Request.ServerVariables("REMOTE_ADDR")
End If
VisitIP="|"&IP&"|"
If instr(1,IPString,VisitIP)>0 Then
'符合禁止的IP执行相应的操作
Response.write "您所在的IP禁止访问"
response.end
End If
%>   

二、把代码修改一下,只允许指定IP防问网站,并执行相应操作:

ASP代码
<%
Dim IP,IPString,VisitIP
'设置IP地址,用“|”隔开
IPString="|192.168.0.42|192.168.0.42|"
'获取IP地址
IP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If IP = "" Then
IP = Request.ServerVariables("REMOTE_ADDR")
End If
VisitIP="|"&IP&"|"
If instr(1,IPString,VisitIP)>0 Then
'符合的IP执行相应的操作
Response.write 您所在的IP可以访问"
else
'不符合的IP执行相应的操作
Response.write "您所在的IP禁止访问"
response.end
End If
%>   

三、实际应用中的使用方法:

然后把上面其中的代码另存为IP.asp文件,然后用下面方法在asp程序页面加入下面代码即可      
通过ASP禁止指定某IP段访问或只允许指定IP段访问网站的代码
<!-- #include file="IP.asp" -->





----------------------分割线 ------------------------------------------------

禁止某IP地址访问自己网站的ASP代码



总有一些讨厌的IP地址来自己网站,那么就用下面的代码来禁止它访问自己的网站吧

把下面代码另存为ip.asp

ASP代码
<%
Dim IP,IPString,VisitIP
'要禁止的IP地址
IPString="|218.58.194.90|218.58.194.218|218.58.194.222|"
'获取IP地址
IP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If IP = "" Then
IP = Request.ServerVariables("REMOTE_ADDR")
End If
VisitIP="|"&IP&"|"
If instr(1,IPString,VisitIP)>0 Then
Response.write "<Script Language=JavaScript>alert('本站禁止您的访问!');</Script>"
response.end
End If
%>

调用方法:把下面这句代码加到你的网页最开始即可          <!--#include file="ip.asp"-->





----------------------分割线------------------------------------------------

允许的IP地址段

ASP代码
<%   
''获取访问者的地址   
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   
%>


页: [1]
查看完整版本: 通过ASP禁止指定某IP段访问或只允许指定IP段访问网站的代码