android - How to distinguish private network from public network in php?
淡淡烟草味2017-05-24 11:30:57
0
3
569
The designated network is a private network, except that all are public networks. How to distinguish it? Anyone who has experience in similar needs can please tell me
The first thing that can be determined is that the internal network IP starts with 127.,10.,192.168.,172., so it is easy to distinguish the internal and external network IPs.
/*
区分是否是内网ip
@param $ip string IP
@return bool true/false
*/
function isLocal($ip){
return preg_match('%^127\.|10\.|192\.168|172\.(1[6-9]|2|3[01])%',$ip);
}
First of all, you need to understand what the special addresses specified in RFC1918 are, and then here is a better answer/q/10...
Is it not possible to determine the IP address?
Why should we distinguish?
The first thing that can be determined is that the internal network IP starts with 127.,10.,192.168.,172., so it is easy to distinguish the internal and external network IPs.