- /**
- * 判断内网IP
- *
- * @param $ip
- *
- * @returns
- */
- function isPrivateIp($ip) {
- //分割字符串
- $token = strtok($ip, '.');
- //组合数组
- while ($token !== false)
- {
- $strIP[] = $token;
- $token = strtok(".");
- }
- //判断IP地址是否合法
- if(count($strIP)!=4)
- {
- return false;
- }
- //判断是否为A类内网IP
- if($strIP[0] == '10')
- {
- if($strIP[1]>=0 && $strIP[1] <= 255)
- {
- if($strIP[2]>=0 && $strIP[2] <= 255)
- {
- if($strIP[3]>=0 && $strIP[3] <= 255)
- {
- return true;
- }
- }
- }
- return false;
- }
- //判断是否为B类内网IP
- if($strIP[0] == '172')
- {
- if($strIP[1] >= 16 && $strIP[1] <= 31)
- {
- if($strIP[2]>=0 && $strIP[2] <= 255)
- {
- if($strIP[3]>=0 && $strIP[3] <= 255)
- {
- return true;
- }
- }
- }
- return false;
- }
- //判断是否为C类内网IP
- if($strIP[0] == '192')
- {
- if($strIP[1] == '168')
- {
- if($strIP[2]>=0 && $strIP[2] <= 255)
- {
- if($strIP[3]>=0 && $strIP[3] <= 255)
- {
- return true;
- }
- }
- }
- return false;
- }
- //错误的IP地址
- return false;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1705201614663.html
来源: http://www.codesnippet.cn/detail/1705201614663.html