堕落的卖猪贩-做人就象做诗,一旦上了境界,就下不来了。

我的Ber客我作主,今天你Ber没有。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理


支持多个通配符,任意通配符位置,可屏蔽IP段和具体IP地址

 1'By zkxp 2/15/2006 http://zkxp.cnblogs.com 
 2'受屏蔽IP地址(段)集合,星号为通配符,通常保存于配置文件中。
 3Const BadIPGroup = "192.168.1.*|202.68.*.*|*.12.55.34|185.*.96.24|127.*.0.1|192.168.0.1"
 4
 5If IsForbidIP(BadIPGroup) = True Then
 6    Response.Write(GetIP &"IP地址禁止访问")
 7    Response.End()
 8End If
 9
10
11'****************************************************************
12'参数vBadIP:要屏蔽的IP段,IP地址集合,用|符号分隔多个IP地址(段)
13'返回Bool:True用户IP在被屏蔽范围,False 反之
14'****************************************************************
15Function IsForbidIP(vBadIP)
16    Dim counter, arrIPPart, arrBadIP, arrBadIPPart, i, j
17    
18    arrBadIP = Split(vBadIP, "|")    
19    arrIPPart = Split(GetIP(), ".")
20    
21    For i = 0 To UBound(arrBadIP)         
22        counter = 0
23        arrBadIPPart = Split(arrBadIP(i), ".")        
24        For j = 0 To UBound(arrIPPart)             
25            If(arrBadIPPart(j)) = "*" or Cstr(arrIPPart(j)) = Cstr(arrBadIPPart(j)) Then            
26                counter = counter + 1            
27            End If            
28        Next        
29        If counter = 4 Then            
30            IsForbidIP = True
31            Exit Function
32        End If        
33    Next
34    IsForbidIP = False
35End Function
36
37'***************
38'返回客户IP地址
39'***************
40Function GetIP()
41    Dim IP
42    IP = Request.ServerVariables("HTTP_X_FORWARDED_FOR"
43    If IP = "" Then IP = Request.ServerVariables("REMOTE_ADDR")
44    GetIP = IP
45End Function


 

另,网上看到的另一个用加减乘除来判断的,有点取巧,单很不灵活,且代码似乎还有问题。仅供参考。

 

<%
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)-1
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*256mod 256)
iip0 
= iip1 &"."& iip2 & "." &iip3 &"."& iip4
Num2IP 
= iip0
end function

userIPnum 
= IP2Num(Request.ServerVariables("REMOTE_ADDR"))

if userIPnum > IP2Num("192.168.3.0"and userIPnum < IP2Num("192.168.3.255"then
response.write (
"<center>此IP被禁止</center>")
response.end
'页面ASP解释结束
end if
%
>
显示内容
posted on 2006-02-15 12:14  堕落的卖猪贩  阅读(2220)  评论(6编辑  收藏  举报