经常有些地方性的 B2B 或者 B2C 网站到处都被批量发布好多垃圾信息,把我的解决办法分 享给大家: 把以下代码加入到你的 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 段来设置。
我就是用这个方法防止外地用户使用流氓
软件恶意到我网站发布信息的! ASP 限制某段 IP 地址访问 function IP2Num(sip) dim str1,str2,str3,str4 dim num IP2Num=0 if isnumeric(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 end if end
function function Num2IP(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) mod 256) iip0 = iip1 &;"."&; iip2 &; "." &;iip3 &;"."&; iip4 Num2IP = iip0 end function
userIPnum = IP2Num(Request.ServerVariables("REMOTE_ADDR")) IP2Num("192.168.0.0") and userIPnum < IP2Num("192.168.0.255") then response.write ("
您的 IP 被禁止") response.end end if 限制 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.write checkip(ip,allowip1,allowip2) function checkip(ip,allowip1,allowip2) dim check(4) checkip=false ipstr=split(ip,".") allow1=split(allowip1,".") allow2=split(allowip2,".") if cint(allow1(0))>cint(allow2(0)) then response.write "你的 IP 地址被禁用 exit function end if
if
userIPnum
>
for i=0 to ubound(ipstr) if cint(allow1(i))
)=true checkip=true exit for else if cint(ipstr(i))cint(allow2(i)) then check(i)=false checkip=false exit for else check(i)=true checkip=true end if end if end if else if cint(allow1(i))>cint(ipstr(i)) or cint(allow1(i))ubound(ipstr) then exit for end if else check(i)=true end if end if next if (check(0)=true and check(1)=true and check(2)=true (cint(allow2