PHP 判断 IP 是否在指定 IP 段内的类
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

说明:
检测一个 IP 地址是否在规定网段内的类。IP 在指定 IP 段范围内的话返回 TRUE 和信息“IP 检测通过”,IP 不在 IP 段范围内则返回 FALSE 和错误信息,错误信息可以从属性“msg”中获得,详见用法和代码。
代码:
<?php class ipCheck { public $ipRangeStr = '10.0.0.1/8'; public $msg = ''; function __construct($ipRangeStr){ !empty($ipRangeStr) ? $this->ipRangeStr = $ipRangeStr : ''; } function check($ip = '') { empty($ip) && $ip = $this->getClientIp(); # 判断检测类型 if (FALSE !== strpos($this->ipRangeStr,'-')){ $type = 'size'; // 简单比大小:10.0.0.1-254 OR 10.0.0.1-10.0.0.254 }else if(FALSE !== strpos($this->ipRangeStr,'/')){ $type = 'mask'; // 掩码比大小:10.0.0.1/24 }else{ $this->msg = '错误的 IP 范围值'; return FALSE; } # 分析 IP 范围 if ('size' === $type){ $ipRangeStr = explode('-',$this->ipRangeStr); $ipAllowStart = $ipRangeStr[0]; $ipAllowEnd = $ipRangeStr[1]; if (FALSE === strpos($ipAllowEnd,'.')){ # 10.0.0.254 OR 254 $ipAllowElmArray = explode('.',$ipAllowStart); $ipAllowEnd = $ipAllowElmArray[0] . '.' . $ipAllowElmArray[1] . '.' . $ipAllowElmArray[2] . '.' . $ipAllowEnd; } }else if ('mask' === $type){ $ipRangeStr = explode('/',$this->ipRangeStr); $ipRangeIP = $ipRangeStr[0]; # 获取掩码中最后一位非零数的值 $ipRangeMask = (int)$ipRangeStr[1]; $maskElmNumber = floor($ipRangeMask/8); # 保留 IP 前几段 $maskElmLastLen = $ipRangeMask % 8; # 255.255.here.0 $maskElmLast = str_repeat(1,8-$maskElmLastLen); $maskElmLast = bindec($maskElmLast); # 掩码中 IP 末段最大值(十进制) // 获取 IP 段开始、结束值 $ipRangeIPElmArray = explode('.',$ipRangeIP); if (0 == $maskElmNumber){ $ipAllowStart = '0.0.0.0'; $ipAllowEnd = $maskElmLast . '.254.254.254'; }else if (1 == $maskElmNumber){ $ipAllowStart = $ipRangeIPElmArray[0] . '.' . '0.0.0'; $ipAllowEnd = $ipRangeIPElmArray[0] . '.' . $maskElmLast . '.254.254'; }else if (2 == $maskElmNumber){ $ipAllowStart = $ipRangeIPElmArray[0] . '.' . $ipRangeIPElmArray[1] . '.' . '0.0'; $ipAllowEnd = $ipRangeIPElmArray[0] . '.' . $ipRangeIPElmArray[1] . '.' . $maskElmLast . '.254'; }else if (3 == $maskElmNumber){ $ipAllowStart = $ipRangeIPElmArray[0] . '.' . $ipRangeIPElmArray[1] . '.' . $ipRangeIPElmArray[2] . '.' . '0'; $ipAllowEnd = $ipRangeIPElmArray[0] . '.' . $ipRangeIPElmArray[1] . '.' . $ipRangeIPElmArray[2] . '.' . $maskElmLast; }else if (4 == $maskElmNumber){ $ipAllowEnd = $ipAllowStart = $ipRangeIP; }else{ $this->msg = '错误的 IP 段数据'; return $this->msg; } }else{ $this->msg = '错误的 IP 段类型'; return $this->msg; } // 检测 IP $ipAllowStart = $this->getDecIp($ipAllowStart); $ipAllowEnd = $this->getDecIp($ipAllowEnd); $ip = $this->getDecIp($ip); if (!empty($ip)){ if ($ip <= $ipAllowEnd && $ip >= $ipAllowStart){ $this->msg = 'IP 检测通过'; return TRUE; }else{ $this->msg = '此为被限制 IP'; return FALSE; } }else{ FALSE === ($this->msg) && $this->msg == '没有提供待检测 IP'; // getClentIp() 是否返回 false return $this->msg; // 没有获取到客户端 IP,返回 } } // 10 进制 IP function getDecIp($ip){ $ip = explode(".", $ip); return $ip[0]*255*255*255+$ip[1]*255*255+$ip[2]*255+$ip[3]; } // 获取客户端 IP function getClientIp(){ if(isset($_SERVER['REMOTE_ADDR'])){ return $_SERVER['REMOTE_ADDR']; }else{ $this->msg = '不能获取客户端 IP'; return FALSE; } } } ?>用法:
$ipCheck = new ipCheck('192.168.1.1-192.168.1.254'); echo (TRUE === $ipCheck ->check('192.168.1.45')) ? '在范围内' : $ipCheck->msg;
$ipCheck = new ipCheck('192.168.1.1-254'); echo (TRUE === $ipCheck ->check('192.168.1.45')) ? '在范围内' : $ipCheck->msg;
$ipCheck = new ipCheck('192.168.1.1/24'); echo (TRUE === $ipCheck ->check('192.168.1.45')) ? '在范围内' : $ipCheck->msg;猜你可能想看的VPS
- 2019 年国内外 PT 站排名及邀请方式全球[VPS测评]
- Evolution Host→€5 月 1GB 内存 20GB SSD 虚拟空间(主机)
- GigsGigsCloud→CLOUDLET K 系列香港大带宽每月 8香港VPS[主机]
- LOVPS 七折 香港 4G 内存 60G 硬盘 3M 带宽 月付 47香港VPS[主机]
- 特价 标准互联→洛杉矶 CN2 GT 线路套餐年付 240 元 BGP 全球[VPS测评]
- 便宜 $7 年 2G 内存 30G SSD 2T 流量@100Mbps 全球[VPS测评]
- 便宜服务器 $49 月 E3-1220 8G 内存 1T 硬盘 20T 全球[VPS测评]
- GigsGigsCloud 新加坡 VPS CLOUDLET SG K1全球[VPS测评]
- WordPress插件:WP User Avatar修改默认头像全球[VPS测评]
- 谨防VPS商家跑路或运营风险,一定要做好数据备份全球[VPS测评]
- hostkvm新上洛杉矶联通线路 VPS 云服务器 AS4837/1G内全球[VPS测评]
- 精美的宠物医院网站WordPress主题 - Valkuta全球[VPS测评]
- 做跨境电商一个月都有多少收入?全球[VPS测评]
- Linux chown -R 指令介绍与使用全球[VPS测评]
- 云计算到底是谁发明的?全球[VPS测评]
- 新华全媒+丨第六届全球跨境电商大会:跨境电商加快拓宽中国贸易新通道全球[VPS测评]
- 野草云怎么样?香港、美国圣何塞高防VPS 1核1G3M 月付19元美国VPS[主机]
- JGKVM促销,美国cn2vps服务器价格再创新低,平均6.5元/月起美国VPS[主机]
- php168数据库配置文件在哪里?如何修改数据库配置文件全球[VPS测评]
- 世界杯季疫情突发,跨境电商订单排队,义乌商人面临难题全球[VPS测评]
- 福州职业技术学院获得世界职业院校技能大赛云计算赛项银牌全球[VPS测评]
- diyvm:香港vps/美国vps/日本vps/2核/2G内存/5M带宽日本VPS[主机]
- 麻花云:2021新春采购节,香港云主机2折起,安徽BGP高防云机,移动大香港VPS[主机]
- 快速云:云计算分类和云计算的特点全球[VPS测评]
- 印象云怎么样?85折香港安畅CN2vps带宽3M月付23元香港VPS[主机]
- 精美的宠物医院网站WordPress主题 - Valkuta全球[VPS测评]
- 漂亮的Css3响应侧边栏菜单,动画风格侧边栏导航菜单样式全球[VPS测评]
- 野草云:香港CN2VPS年付36折,137元/年;独立服务器350元月起独立服务器[U]
- 锤子云:香港CN2云服务器15元/月,三网CN2线路回程,建站vps专属香港VPS[主机]
- 触摸云:香港CN2 GIA云服务器,1核/1G/20G/10M,促销月付香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-220202.html











