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]历史优惠活动内容
猜你可能想看的VPS
- 优质低价 racknerd→“白色情人节”便宜 VPS 限量 100 个全球[VPS测评]
- js 实时显示时间(自动变化)代码全球[VPS测评]
- 便宜服务器 35 美元 月 E5-2650L 16G 1T 4IP 10全球[VPS测评]
- HostMem→$12.99 年 1GB 内存 60GB 空间 500G虚拟空间(主机)
- 便宜 $13.99 年 512M 内存 20G SSD 250G 流量 全球[VPS测评]
- SoftShellWeb→首月 0.01-0.02 美元 KVM 主机 全球[VPS测评]
- AuroraCloud→240 元 月 1GB 内存 30GB SSD 虚拟空间(主机)
- ZJI 日本站群服务器 8 折→1560 元 E3-1240Lv3 16站群服务器[IP]
- php 判断两个数组是否相等全球[VPS测评]
- $6 月 2 核 CPU 5G 内存 100G SSD 100Mbps 全球[VPS测评]
- 疯狂猜成语 图猜成语一个迷宫一只象棋的马走出来是什么成语?全球[VPS测评]
- Vultr VPS主机如何添加自定义防火墙规则全球[VPS测评]
- HydroVPS 加拿大绿色环保VPS 1G内存/15GNvme/500全球[VPS测评]
- CloudCone 512M 内存 10G SSD 512G流量 洛杉矶全球[VPS测评]
- CloudCone 512 内存 10G HDD 1T流量 洛杉矶 MC全球[VPS测评]
- 网站迁移服务器是如何运作的?新手站长看完这篇文章,很容易就能看完。全球[VPS测评]
- 美国信贷市场的痛苦将不亚于上世纪70年代的通胀时期美国VPS[主机]
- 做跨境电商,如何打造私域流量?全球[VPS测评]
- JGKVM促销,美国cn2vps服务器价格再创新低,平均6.5元/月起美国VPS[主机]
- 向日葵-漏洞科普:海外云服务器三种漏洞修复方法快收藏起来!全球[VPS测评]
- 轻云互联,香港美国vps终身8.8折17元/月起,cn2直连线路,50G美国VPS[主机]
- VPS到底有什么用?全球[VPS测评]
- 樊云:香港CN2节点vps,最低月付22.5起,带宽5Mbps起步香港VPS[主机]
- 创e云:香港CN2VPS优惠专区,4核/4G/20G/2Mbps不限流量香港VPS[主机]
- 腾讯云免备案海外服务器:高速网络免备案,助力企业出海!全球[VPS测评]
- 源云主机,香港沙田VPS CN2 1核1G 24/月 NTT 50M 9香港VPS[主机]
- 三优云uuuvps:春节促销,香港cn2 vps低至168元/年,多款配香港VPS[主机]
- OneTechCloud易科云双十二优惠:香港/美国CN2全场VPS季付美国VPS[主机]
- DiyVM:香港服务器499元起,香港/美国vps五折/2G内存,月付6美国VPS[主机]
- 葫芦云:香港CN2/洛杉矶GIA高防vps,24元/月;1核/1GB/2虚拟空间(主机)
转载请注明原文地址:http://140.238.13.167:12355/read-157538.html











