支持多个通配符,任意通配符位置,可屏蔽IP段和具体IP地址
1
'By zkxp 2/15/2006 http://zkxp.cnblogs.com
2
'受屏蔽IP地址(段)集合,星号为通配符,通常保存于配置文件中。
3
Const BadIPGroup = "192.168.1.*|202.68.*.*|*.12.55.34|185.*.96.24|127.*.0.1|192.168.0.1"
4![]()
5
If IsForbidIP(BadIPGroup) = True Then
6
Response.Write(GetIP &"IP地址禁止访问")
7
Response.End()
8
End If
9![]()
10![]()
11
'****************************************************************
12
'参数vBadIP:要屏蔽的IP段,IP地址集合,用|符号分隔多个IP地址(段)
13
'返回Bool:True用户IP在被屏蔽范围,False 反之
14
'****************************************************************
15
Function 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
35
End Function
36![]()
37
'***************
38
'返回客户IP地址
39
'***************
40
Function GetIP()
41
Dim IP
42
IP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
43
If IP = "" Then IP = Request.ServerVariables("REMOTE_ADDR")
44
GetIP = IP
45
End Function
'By zkxp 2/15/2006 http://zkxp.cnblogs.com 2
'受屏蔽IP地址(段)集合,星号为通配符,通常保存于配置文件中。3
Const BadIPGroup = "192.168.1.*|202.68.*.*|*.12.55.34|185.*.96.24|127.*.0.1|192.168.0.1"4

5
If IsForbidIP(BadIPGroup) = True Then6
Response.Write(GetIP &"IP地址禁止访问")7
Response.End()8
End If9

10

11
'****************************************************************12
'参数vBadIP:要屏蔽的IP段,IP地址集合,用|符号分隔多个IP地址(段)13
'返回Bool:True用户IP在被屏蔽范围,False 反之14
'****************************************************************15
Function IsForbidIP(vBadIP)16
Dim counter, arrIPPart, arrBadIP, arrBadIPPart, i, j17
18
arrBadIP = Split(vBadIP, "|") 19
arrIPPart = Split(GetIP(), ".")20
21
For i = 0 To UBound(arrBadIP) 22
counter = 023
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 = True31
Exit Function32
End If 33
Next34
IsForbidIP = False35
End Function36

37
'***************38
'返回客户IP地址39
'***************40
Function GetIP()41
Dim IP42
IP = Request.ServerVariables("HTTP_X_FORWARDED_FOR") 43
If IP = "" Then IP = Request.ServerVariables("REMOTE_ADDR")44
GetIP = IP45
End 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*256) mod 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
%>
显示内容
浙公网安备 33010602011771号