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
- StackPath→香港 韩国 日本 新加坡等 CDN 并有全球节点 D日本VPS[主机]
- 超优惠 磐石云元旦特惠→1 核 2G 内存 50G 硬盘 2G 防御 三全球[VPS测评]
- 新商家慎重-YonZy→66 元 年 512MB 内存 6GB SSD 虚拟空间(主机)
- XenSpec→$2.4 月 KVM-1GB 10GB 无限流量(1Gb全球[VPS测评]
- 疯狂猜成语 图猜成语一只鹅和一只鸡在说话是什么成语?全球[VPS测评]
- DediPath→4 折优惠码 $2.3 月 2 核 1G 100G S美国VPS[主机]
- bash: scp: command not found 错误解决办法全球[VPS测评]
- 真实测评 云邦互联 E38 核 16GB 2TB 硬盘 10M 宽带 C全球[VPS测评]
- 符码 CDN→香港节点 500GB 月流量 月付 1 元;800GB 月香港VPS[主机]
- 哪些国外VPS商家可以支付宝付款?全球[VPS测评]
- WordPress博客的百度快照怎么快速更新?全球[VPS测评]
- 优化YSlow add expires headers项目提升博客访问速全球[VPS测评]
- HostYun 全场9折优惠 月付15.3元 大带宽 512M 10G 全球[VPS测评]
- towwwer.host-俄罗斯1C1G15GB 不限流量 145卢布 全球[VPS测评]
- UOVZ 384M 内存 5G SSD 2T单向流量 上海NAT → 月全球[VPS测评]
- 又一跨境电商产业园落户!中山石岐签约引进16个项目全球[VPS测评]
- MoonVM,香港HKT家宽VPS服务器,动态IP一键切换,1G带宽任意香港VPS[主机]
- 适合美容院的正能量抖音文案,唯美阳光,正能量满满全球[VPS测评]
- SugarHosts糖果主机双十一大促销 虚拟主机最高6折优惠全球[VPS测评]
- 六一云:香港CMI VPS,10G防御,1核/1G/40G SSD/15香港VPS[主机]
- edgeNAT:全场6折优惠,韩国CN2、美国CN2、香港CN2月付36韩国VPS[主机]
- 六六云:香港BGP云服务器/80M大带宽,月付45元起,5折优惠码可用香港VPS[主机]
- 特网云:香港高防CN2云服务器,新加坡/美国/洛杉矶/英国/荷兰等,5M美国VPS[主机]
- 易科云:香港cn2 vps,洛杉矶 cn2云主机;全场月付9折/季付8折香港VPS[主机]
- 影速科技怎么样?影速科技海外云服务器价格多少钱?全球[VPS测评]
- 8K超高清记录中国空间站凌月:天和核心舱与问天实验舱组成“土”字结构虚拟空间(主机)
- RangCloud:山东BGP NAT VPS,19元/首月,大带宽VP香港VPS[主机]
- tmhhost:香港安畅机房三网CN2 GIA VPS,月付45起;美国美国VPS[主机]
- 奇异互动,2核4G香港云服务器仅655元/年,注册领550元新手大礼包香港VPS[主机]
- 2.45亿、成都公安(大数据基座-服务器)采购:神码、虹信、海康、超聚变全球[VPS测评]
转载请注明原文地址:http://140.238.13.167:12355/read-205340.html











