這篇文章主要介紹了asp實作檢查ip位址是否為內網或私有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
實現代碼:
<%function IpToNumber(ip)'IP位址轉為數位arr=split(ip,.) IpToNumber=256*256*256*clng(arr(0))+256*256*clng(arr(1))+256 *clng(arr(2))+clng(arr(3))end functionfunction IsPrivateIp(ip)'判斷給定的IP位址是否內網/私有ip位址if instr(ip,127.)=1 then'回環IP位址IsPrivateIp=true:exit function end if ABegin=IpToNumber(10.0.0.0):AEnd=IpToNumber(10.255.255.255)'A類別私有IP位址BBegin=IpToNumber(172.16.0.0):BEnd=IpToNumber(1 72.31.255.255)'B類私有IP位址CBegin=IpToNumber(192.168.0.0):CEnd=IpToNumber(192.168.255.255)'C類私有IP位址IpNum=IpToNumber(ip) IsPrivateIp=(ABegin<=IpNum and IpNum<=AEnd) or (BBegin<=IpNum and IpNum<=BEnd) or (CBegin<=IpNum and IpNum<=CEnd)end functionResponse.Write IsPrivateIp(11.255.255.255)&<br>'falseResponse.Write IsPrivateIp(182.255.255.255)&<br>'falseResponse.Write IsPrivateIp(172.30.255. IsPrivateIp(192.168.205.2)&<br>'trueResponse.Write IsPrivateIp(127.168.205.2)'true %>