asp实现检查ip地址是否为内网或者私有ip地址的代码分享
asp检查ip地址是否为私有/内网ip地址源代码。
内网/私有IP地址网段如下,还有127开头的回环地址:
10.0.0.0-10.255.255.255
172.16.0.0—172.31.255.255
192.168.0.0-192.168.255.255
实现代码:
<% functionIpToNumber(ip)'IP地址转为数字 arr=split(ip,".") IpToNumber=256*256*256*clng(arr(0))+256*256*clng(arr(1))+256*clng(arr(2))+clng(arr(3)) endfunction functionIsPrivateIp(ip)'判断给定的IP地址是否内网/私有ip地址 ifinstr(ip,"127.")=1then'回环IP地址 IsPrivateIp=true:exitfunction endif ABegin=IpToNumber("10.0.0.0"):AEnd=IpToNumber("10.255.255.255")'A类私有IP地址 BBegin=IpToNumber("172.16.0.0"):BEnd=IpToNumber("172.31.255.255")'B类私有IP地址 CBegin=IpToNumber("192.168.0.0"):CEnd=IpToNumber("192.168.255.255")'C类私有IP地址 IpNum=IpToNumber(ip) IsPrivateIp=(ABegin<=IpNumandIpNum<=AEnd)or(BBegin<=IpNumandIpNum<=BEnd)or(CBegin<=IpNumandIpNum<=CEnd) endfunction Response.WriteIsPrivateIp("11.255.255.255")&"<br>"'false Response.WriteIsPrivateIp("182.255.255.255")&"<br>"'false Response.WriteIsPrivateIp("172.30.255.255")&"<br>"'true Response.WriteIsPrivateIp("192.168.205.2")&"<br>"'true Response.WriteIsPrivateIp("127.168.205.2")'true %>