'将十进制转为二进制字符串
function dec2bin(octNumber)
vara=octNumber
do
dec2bin=cstr(vara mod 2) & dec2bin
vara=vara \ 2
loop until vara=0
end function
'将二进制字符串填充为8位
function pad(str)
pad=right("00000000" & str,8)
end function
'判断是否是一个IP地址
function isIp(ipadd)
isIp=false
set oReg=new RegExp
oReg.IgnoreCase=true
oReg.global=true
oReg.Pattern="(\d{1,4}\.\d{1,4}\.\d{1,4}\.\d{1,4})|(\d{1,4}\.\d{1,4}\.\d{1,4}\.\d{1,4}\/\d{1,2})"
if oReg.test(ipadd) then isIp=true
set oReg=nothing
end function
'其中UserIP是我们要检测的IP
'NetIP是要检测的网段或某个IP,用xxx.xxx.xxx.xxx/N来表示网段,其中N表示子网掩码位数
'注,该程序是环球万维原创程序,所以如果您要转载,请保留出处信息,谢谢.
'程序设计:环球万维,专业提供域名注册,虚拟主机服务
'网址:http://www.netInter.cn
'以上信息与文章正文是不可分割的一部分,所以如果您要转载本文章,您必须保留以上信息.
Function check_ip(UserIp,NetIP)
currentip=UserIp
collection_ips=split(iplist,",") '将网络按点分割成4段
check_ip=false '初始函数值,false假设IP不在这网段
NetIP=trim(NetIP)
slashPos=inStr(NetIP,"/")
if slashPos=0 then '网段没含有/符号,他只是一个IP,所以比较比个字符串是否相同就可以了
if NetIP=currentip then
check_ip=true 'check_ip=true表示IP相等
exit function
end if
else
netRang=mid(NetIP,slashPos+1) '得到/后边的数字
if not isNumeric(netRang) then '/后边不是数字,格式不正确
exit function
end if
netRang=cint(netRang) '将字符转为数字
if netRang>31 then
exit function '/后的数字不能超过32位
end if
ipsets=split(currentip,".") '将用户IP按点分成四段
C_IP_BIN=pad(dec2bin(ipsets(0))) & pad(dec2bin(ipsets(1))) & pad(dec2bin(ipsets(2))) & pad(dec2bin(ipsets(3)))
'上边这行是将用户IP地址手工转换为对应的一个32个字符长的二进制
ipsets=split(NetIP,".") '按上边的过程将网段IP同样转为32个字符长的二进制
sPos=instr(ipsets(3),"/") '最后一点格式应该是 数字/数字
if sPos=0 then
exit function
end if
ipsets(3)=left(ipsets(3),sPos-1) '得到最后一段/前边的数字
S_IP_BIN=pad(dec2bin(ipsets(0))) & pad(dec2bin(ipsets(1))) & pad(dec2bin(ipsets(2))) & pad(dec2bin(ipsets(3)))
'将其转换为32个字符长的二进制
if left(C_IP_BIN,netRang) = left(S_IP_BIN,netRang) then '比较网段络是否相同就可以判断用户IP否属于某个网段了
check_ip=true
end if
最新相关文章
发表评论