ASP代码中如何屏蔽ip地址.docx
- 文档编号:24650189
- 上传时间:2023-05-29
- 格式:DOCX
- 页数:19
- 大小:20.82KB
ASP代码中如何屏蔽ip地址.docx
《ASP代码中如何屏蔽ip地址.docx》由会员分享,可在线阅读,更多相关《ASP代码中如何屏蔽ip地址.docx(19页珍藏版)》请在冰豆网上搜索。
ASP代码中如何屏蔽ip地址
经常有些地方性的B2B或者B2C网站到处都被批量发布好多垃圾信息,把我的解决办法分享给大家:
把以下代码加入到你的ASP页面就可以测试到效果了:
<%
'受屏蔽IP地址(段)集合,星号为通配符,通常保存于配置文件中。
ConstBadIPGroup="192.168.1.*|202.68.*.*|*.12.55.34|185.*.96.24|127.*.0.1|192.168.0.1"
IfIsForbidIP(BadIPGroup)=TrueThen
Response.Write(GetIP&"IP地址禁止访问")
Response.End()
EndIf
'参数vBadIP:
要屏蔽的IP段,IP地址集合,用|符号分隔多个IP地址(段)
'返回Bool:
True用户IP在被屏蔽范围,False反之
FunctionIsForbidIP(vBadIP)
Dimcounter,arrIPPart,arrBadIP,arrBadIPPart,i,j
arrBadIP=Split(vBadIP,"|")
arrIPPart=Split(GetIP(),".")
Fori=0ToUBound(arrBadIP)
counter=0
arrBadIPPart=Split(arrBadIP(i),".")
Forj=0ToUBound(arrIPPart)
If(arrBadIPPart(j))="*"orCstr(arrIPPart(j))=Cstr(arrBadIPPart(j))Then
counter=counter+1
EndIf
Next
Ifcounter=4Then
IsForbidIP=True
ExitFunction
EndIf
Next
IsForbidIP=False
EndFunction
''返回客户IP地址
FunctionGetIP()
DimIP
IP=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
IfIP=""ThenIP=Request.ServerVariables("REMOTE_ADDR")
GetIP=IP
EndFunction
%>
这样就可以限制网站访问的IP段了,你可以根据地区的IP段来设置。
我就是用这个方法防止外地用户使用流氓软件恶意到我网站发布信息的!
ASP限制某段IP地址访问
functionIP2Num(sip)
dimstr1,str2,str3,str4
dimnum
IP2Num=0
ifisnumeric(left(sip,2))then
str1=left(sip,instr(sip,".")-1)
sip=mid(sip,instr(sip,".")+1)
str2=left(sip,instr(sip,".")-1)
sip=mid(sip,instr(sip,".")+1)
str3=left(sip,instr(sip,".")-1)
str4=mid(sip,instr(sip,".")+1)
num=cint(str1)*256*256*256+cint(str2)*256*256+cint(str3)*256+cint(str4)
IP2Num=num
endif
end
functionfunctionNum2IP(nip)
iip1=int(nip/256/256/256)
iip2=int((nip-iip1*256*256*256)/256/256)
iip3=int((nip-iip1*256*256*256-iip2*256*256)/256)
iip4=int((nip-iip1*256*256*256-iip2*256*256-iip3*256)mod256)
iip0=iip1&"."&iip2&"."&iip3&"."&iip4
Num2IP=iip0
endfunction
userIPnum=IP2Num(Request.ServerVariables("REMOTE_ADDR"))ifuserIPnum>IP2Num("192.168.0.0")anduserIPnum response.write("
response.end
endif
限制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
response.write"你的IP地址被禁用
exitfunction
endif
fori=0toubound(ipstr)
ifcint(allow1(i)) ifcint(allow1(i))=cint(ipstr(i))then check(i)=true checkip=true exitfor else ifcint(ipstr(i)) 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)) 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地址访问网站并跳转指定网址 将下面的代码保存为INDEX.ASP放到网站根目录下,原网站首页改名为INDE.HTM XML/HTML代码 <% FunctionRegReplace(vStr,vReg,vCode) DimRe SetRe=NewRegExp Re.Pattern=vReg Re.IgnoreCase=True Re.Global=True IfRe.Test(vStr)Then vStr=Re.Replace(vStr,vCode) EndIf RegReplace=vStr EndFunction dimweburl,HostName dimconna,connstra,dba dba="useraddress.mdb" setconna=Server.CreateObject("ADODB.CONNECTION") connstra="Provider=Microsoft.Jet.OLEDB.4.0;dataSource="&Server.MapPath(dba) conna.openconnstra user_ip=Request.ServerVariables("REMOTE_ADDR")'取得访问者IP userip_ary=split(user_ip,".") tmp_userip=userip_ary(0)*256*256*256+userip_ary (1)*256*256+userip_ary (2)*256+userip_ary(3)'按规定转换IP为数字 sql="selectcount(*)fromdv_addresswhereip1<="&tmp_userip&"andip2>="&tmp_userip iCount=conna.Execute(sql)(0) ifiCount=0then'外地显示内容 response.redirect("inde.htm") else'北京显示内容 response.redirect("http: //北京地区访问跳转的网址") endif %> 其中有个ACCESS数据库,和INDEX.ASP放在同级目录下,全打包下载: 禁止ip.rar 还有一种方法是直接限制某个IP或是某个IP段访问网站,相对来说比较简单,直接在ASP网页中加入如下代码即可: XML/HTML代码 <% '受屏蔽IP地址(段)集合,星号为通配符,通常保存于配置文件中。 ConstBadIPGroup="192.168.1.*|202.68.*.*|*.12.55.34|185.*.96.24|127.*.0.1|192.168.0.1" IfIsForbidIP(BadIPGroup)=TrueThen Response.Write(GetIP&"IP地址禁止访问") Response.End() EndIf '参数vBadIP: 要屏蔽的IP段,IP地址集合,用|符号分隔多个IP地址(段) '返回Bool: True用户IP在被屏蔽范围,False反之 FunctionIsForbidIP(vBadIP) Dimcounter,arrIPPart,arrBadIP,arrBadIPPart,i,j arrBadIP=Split(vBadIP,"|") arrIPPart=Split(GetIP(),".") Fori=0ToUBound(arrBadIP) counter=0 arrBadIPPart=Split(arrBadIP(i),".") Forj=0ToUBound(arrIPPart) If(arrBadIPPart(j))="*"orCstr(arrIPPart(j))=Cstr(arrBadIPPart(j))Then countercounter=counter+1 EndIf Next Ifcounter=4Then IsForbidIP=True ExitFunction EndIf Next IsForbidIP=False EndFunction ''返回客户IP地址 FunctionGetIP() DimIP IP=Request.ServerVariables("HTTP_X_FORWARDED_FOR") IfIP=""ThenIP=Request.ServerVariables("REMOTE_ADDR") GetIP=IP EndFunction %> ASP过滤IP限制访问网站的方法 源程序示例如下: 以下为引用的内容: <html> <head> <metahttp-equiv=“Content-Type”content=“text/html;charset=gb_2312-80”> <metaname=“GENERATOR”content=“MicrosoftFrontPageExpress2.0”> <style> <! -- .as{line-height: 15px;font-size: 9pt} a: hover{color: rgb(0,51,240);text-decoration: underline} .p9{font-family: “宋体”;font-size: 9pt;line-height: 15pt} .p12{font-family: “宋体”;font-size: 12pt;line-height: 18pt} a: link{text-decoration: none;} a: visited{text-decoration: none;} a: hover{text-decoration: underline;font-size: 125%;color: blue} --> </style> <title>ASP页面防火墙功能演示</title> </head> <bodybackground=“back.jpg”> <% ′使用Request.ServerVariables(“REMOTE_ADDR”)得到IP地址并保存在变量rip中 rip=Request.ServerVariables(“REMOTE_ADDR”) strip=cstr(rip) ′取得IP地址第三个段的值并保存到strip中 fori=1to2 strip=right(strip,len(strip)-instr(1,strip,“.”)) next strip=left(strip,instr(1,strip,“.”)-1) ′IP地址有效性检验及密码验证,包括两方面的内容: ′如果IP地址符合则通过验证;如果IP地址不符合则检验输入的密码是否正确(此处密码为“asp”) if(left(rip,5)<>“127.1”orstrip<“1”orstrip>“50”)andrequest(“Passwd”)<>“asp”then %> <p><fontcolor=“#FF0000”>对不起,你的IP是<%=rip%>,本页面可以访问的IP是127.1.1.*到127.1.50.*之间,如果你是本单位内部网的用户,请确认你的浏览器没有使用代理! <BR></font></p> <formaction=“demo.asp”method=“POST”id=form1name=form1> <p>请输入访问密码: <inputtype=“password”name=“Passwd”><inputtype=“submit”value=“确认”name=“B1”>; </p> </form> <%else%> ′合法用户可以访问的页面,在此可以加入任何信息 祝贺您,您已经顺利通过了页面的安全认证,可以直接使用本站点的资源! <%endif%> </body> </html> 实际使用只要稍微修改上面的程序(如IP地址等信息)就可以了,当然这只是在一个页面中实现了安全防范功能,如果一个网站有多个页面的话,可以设置一个session变量来对用户进行标志,以在后面的页面中进行判断。 ASP限制IP访问的小代码 ip_start="61.143.129.0" ip_end="61.143.133.255" ip_start_arr=split(ip_start,".") ip_end_arr=split(ip_end,".") ip_start_str="" ip_end_str="" ip_user_str="" fori=0to3 ip_start_str=ip_start_str&right(Cstr(formatnumber(Cint(ip_start_arr(i))/1000,3)),3) ip_end_str=ip_end_str&right(Cstr(formatnumber(Cint(ip_end_arr(i))/1000,3)),3) next '测试客户端是否用代理 userip=Request.ServerVariables("HTTP_X_FORWARDED_FOR") Ifuserip=""Thenuserip=Request.ServerVariables("REMOTE_ADDR") ip_user_arr=split(userip,".") fori=0to3 ip_user_str=ip_user_str&right(Cstr(formatnumber(Cint(ip_user_arr(i))/1000,3)),3) next ifCDbl(ip_user_str)>CDbl(ip_end_str)orCDbl(ip_user_str) response.write"超出访问范围" endif [推荐]ASP网站IP访问限制函数 '****************************** 'FunctionCheckIp(cInput_Ip,cBound_Ip) 'Createdbyqqdao,qqdao@263.Net2001/11/28 '说明: 首先需要根据;号循环,然后判断是否含有"-",如果有则进行拆分处理,最后判断是否在范围内 '参数: cInput_Ip,代检查的ip 'cBound_Ip,给定的范围格式为,单个ip,和范围ip,范围ip最后使用”-“分割,如果是“*”则必须放到最后一位 '每个范围后添加": ALLOW"表示允许登陆,添加": REFUSE"表示拒绝登陆。 多个范围用”;“隔开 ' 例如192.168.1*.*: ALLOW;192.168.1.1: ALLOW;192.168.1.1-10: REFUSE" '返回值: true/false '更新: 2001/12/05支持ALLOW,REFUSE支持’*‘,不想对? 支持,因为和*差不多 '****************************** functionCheckIp(cInput_Ip,cBound_Ip) dimcSingle_Ip,cTemp_IP,cStart_IP,cEnd_Ip CheckIp=false cSingle_Ip=split(cBound_Ip,";") fori=0toubound(cSingle_Ip) ifInstr(cSingle_Ip(i),"REFUSE")<>0then'就是拒绝了 cTemp_IP=left(cSingle_Ip(i),instr(cSingle_Ip(i),": ")-1) ifInstr(cTemp_IP,"*")<>0then'是宽范围 cStart_IP=left(cTemp_IP,instr(cTemp_IP,"*")-1) ifleft(cInput_Ip,len(cStart_IP))=cStart_IPthen CheckIp=false exitfunction endif endif ifInstr(cTemp_IP,"-")=0then cStart_IP=cTemp_IP cEnd_Ip=cTemp_IP else cStart_IP=left(cTemp_IP,instr(cTemp_IP,"-")-1) cEnd_Ip= left(cStart_IP,InStrRev(cStart_IP,".")-1)+"."+mid(cTemp_IP,instr(cTemp_IP,"-")+1) endif ifIp2Str(cInput_Ip)>=Ip2Str(cStart_IP)and Ip2Str(cInput_Ip)<=Ip2Str(cEnd_Ip)then CheckIp=false exitfunction endif elseifInstr(cSingle_Ip(i),"ALLOW")<>0then'允许 cTemp_IP=left(cSingle_Ip(i),instr(cSingle_Ip(i),": ")-1) ifInstr(cTemp_IP,"*")<>0then'是宽范围 cStart_IP=left(cTemp_IP,instr(cTemp_IP,"*")-1) ifleft(cInput_Ip,len(cStart_IP))=cStart_IPthen CheckIp=true endif endif ifInstr(cTemp_IP,"-")=0then cStart_IP=cTemp_IP cEnd_Ip=cTemp_IP else cStart_IP=left(cTemp_IP,instr(cTemp_IP,"-")-1) cEnd_Ip= left(cStart_IP,InStrRev(cStart_IP,".")-1)+"."+mid(cTemp_IP,instr(cTemp_IP,"-")+1) endif ifIp2Str(cInput_Ip)>=Ip2Str(cStart_IP)and Ip2Str(cInput_Ip)<=Ip2Str(cEnd_Ip)then CheckIp=true else CheckIp=false endif endif next endfunction '****************************** 'FunctionIp2Str(cIp)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ASP 代码 如何 屏蔽 ip 地址