ASP中实现限制IP的函数详解
程序员文章站
2023-11-21 17:01:46
'****************************** 'function checkip(cinput_ip,cbound_ip) 'created&n...
'******************************
'function checkip(cinput_ip,cbound_ip)
'created by qqdao, qqdao@263.net 2001/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支持'*‘,不想对?支持,因为和*差不多
'******************************
function checkip(cinput_ip,cbound_ip)
dim csingle_ip,ctemp_ip,cstart_ip,cend_ip
checkip = false
csingle_ip=split(cbound_ip,";")
for i=0 to ubound(csingle_ip)
if instr(csingle_ip(i),"refuse") <> 0 then “就是拒绝了
ctemp_ip = left(csingle_ip(i),instr(csingle_ip(i),":")-1)
if instr(ctemp_ip,"*") <> 0 then “是宽范围
cstart_ip = left(ctemp_ip,instr(ctemp_ip,"*")-1)
if left(cinput_ip,len(cstart_ip))=cstart_ip then
checkip = false
exit function
end if
end if
if instr(ctemp_ip,"-") = 0 then
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)
end if
if ip2str(cinput_ip)>=ip2str(cstart_ip) and ip2str(cinput_ip)<=ip2str(cend_ip) then
checkip = false
exit function
end if
elseif instr(csingle_ip(i),"allow") <> 0 then “允许
ctemp_ip = left(csingle_ip(i),instr(csingle_ip(i),":")-1)
if instr(ctemp_ip,"*") <> 0 then “是宽范围
cstart_ip = left(ctemp_ip,instr(ctemp_ip,"*")-1)
if left(cinput_ip,len(cstart_ip))=cstart_ip then
checkip = true
end if
end if
if instr(ctemp_ip,"-") = 0 then
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)
end if
if ip2str(cinput_ip)>=ip2str(cstart_ip) and ip2str(cinput_ip)<=ip2str(cend_ip) then
checkip =true
else
checkip =false
end if
end if
next
end function
'******************************
'function ip2str(cip)
'created by qqdao, qqdao@263.net 2001/11/28
'参考动网ip算法
'参数:cip ip地址
'返回值: 转换后数值
'******************************
function ip2str(cip)
dim str1,str2,str3,str4
dim cip_temp
if cip="127.0.0.1" then cip="192.168.0.1"
str1=left(cip,instr(cip,".")-1)
cip_temp=mid(cip,instr(cip,".")+1)
str2=left(cip_temp,instr(cip_temp,".")-1)
cip_temp=mid(cip_temp,instr(cip_temp,".")+1)
str3=left(cip_temp,instr(cip_temp,".")-1)
str4=mid(cip_temp,instr(cip_temp,".")+1)
if isnumeric(str1)=0 or isnumeric(str2)=0 or isnumeric(str3)=0 or isnumeric(str4)=0 then
else
ip2str=cint(str1)*256*256*256+cint(str2)*256*256+cint(str3)*256+cint(str4)-1
end if
end function
'function checkip(cinput_ip,cbound_ip)
'created by qqdao, qqdao@263.net 2001/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支持'*‘,不想对?支持,因为和*差不多
'******************************
function checkip(cinput_ip,cbound_ip)
dim csingle_ip,ctemp_ip,cstart_ip,cend_ip
checkip = false
csingle_ip=split(cbound_ip,";")
for i=0 to ubound(csingle_ip)
if instr(csingle_ip(i),"refuse") <> 0 then “就是拒绝了
ctemp_ip = left(csingle_ip(i),instr(csingle_ip(i),":")-1)
if instr(ctemp_ip,"*") <> 0 then “是宽范围
cstart_ip = left(ctemp_ip,instr(ctemp_ip,"*")-1)
if left(cinput_ip,len(cstart_ip))=cstart_ip then
checkip = false
exit function
end if
end if
if instr(ctemp_ip,"-") = 0 then
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)
end if
if ip2str(cinput_ip)>=ip2str(cstart_ip) and ip2str(cinput_ip)<=ip2str(cend_ip) then
checkip = false
exit function
end if
elseif instr(csingle_ip(i),"allow") <> 0 then “允许
ctemp_ip = left(csingle_ip(i),instr(csingle_ip(i),":")-1)
if instr(ctemp_ip,"*") <> 0 then “是宽范围
cstart_ip = left(ctemp_ip,instr(ctemp_ip,"*")-1)
if left(cinput_ip,len(cstart_ip))=cstart_ip then
checkip = true
end if
end if
if instr(ctemp_ip,"-") = 0 then
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)
end if
if ip2str(cinput_ip)>=ip2str(cstart_ip) and ip2str(cinput_ip)<=ip2str(cend_ip) then
checkip =true
else
checkip =false
end if
end if
next
end function
'******************************
'function ip2str(cip)
'created by qqdao, qqdao@263.net 2001/11/28
'参考动网ip算法
'参数:cip ip地址
'返回值: 转换后数值
'******************************
function ip2str(cip)
dim str1,str2,str3,str4
dim cip_temp
if cip="127.0.0.1" then cip="192.168.0.1"
str1=left(cip,instr(cip,".")-1)
cip_temp=mid(cip,instr(cip,".")+1)
str2=left(cip_temp,instr(cip_temp,".")-1)
cip_temp=mid(cip_temp,instr(cip_temp,".")+1)
str3=left(cip_temp,instr(cip_temp,".")-1)
str4=mid(cip_temp,instr(cip_temp,".")+1)
if isnumeric(str1)=0 or isnumeric(str2)=0 or isnumeric(str3)=0 or isnumeric(str4)=0 then
else
ip2str=cint(str1)*256*256*256+cint(str2)*256*256+cint(str3)*256+cint(str4)-1
end if
end function