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
- Altınsoft→$63 月 E3-1240v3 16GB 内存 25全球[VPS测评]
- DogYun→圣何塞 CN2 GIA 线路经典云月付 15.6 元起 动全球[VPS测评]
- CentOS 网卡错误→Determining if ip addres全球[VPS测评]
- input 输入框自动获取焦点全球[VPS测评]
- Meihezi→59 元 月 KVM-1GB 30GB 5M 无限 南非全球[VPS测评]
- GinerNet→€19.99 月 4GB 内存 50GB NVMe 空虚拟空间(主机)
- 新商家慎重-伏羲→19.2 月 1GB 内存 40GB SSD 硬盘 不全球[VPS测评]
- 倒着写的人字和马字是什么成语?全球[VPS测评]
- JS 完美校验身份证号有效性函数(亲测)全球[VPS测评]
- 域名价格监控及注册查询网站汇总全球[VPS测评]
- Hetzner→€4.15 月 AMD EPYC 2GB 内存 40GB虚拟空间(主机)
- 非常便宜 centerhop→1 核 768M 15G SSD 1T 流全球[VPS测评]
- Moack.co.kr→$39 月-Dual E5 2620V3 32G韩国VPS[主机]
- WordPress建立数据库连接时出错怎么办?全球[VPS测评]
- 性价比高便宜的域名 SSL 证书,可选BaiduTrust、Positi全球[VPS测评]
- Webhosting1st波兰高防VPS首付五折,$3起/月全球[VPS测评]
- ConoHa 新加坡小鸡评测 不能看 Netflix全球[VPS测评]
- V.PS → 日本(东京) 软银 VPS 年付85折 29.95欧元起日本VPS[主机]
- 创意IT服务公司主页网站模板 - Softets全球[VPS测评]
- 中行升级服务力促跨境电商发展全球[VPS测评]
- 常见的CN2,GIA,CIA 各个网络线路解释全球[VPS测评]
- 人们为啥不爱吃海底捞了?海底捞上半年亏损达2.97亿 网友神评扎心全球[VPS测评]
- 人们为啥不爱吃海底捞了?海底捞上半年亏损达2.97亿 网友神评扎心全球[VPS测评]
- UOVZ怎么样?香港30M大带宽VPS 直连线路月付50元香港VPS[主机]
- 樊云:香港CN2节点vps,最低月付22.5起,带宽5Mbps起步香港VPS[主机]
- 六一云:国内BGP高防vps套餐,1核/1G/2Mbps/香港CN2,月香港VPS[主机]
- LOCVPS:全球云美国洛杉矶CN2 VPS,香港云地Xen VPS七折美国VPS[主机]
- 菠萝云服务器怎么样?香港云主机BGP、CN2,2核1G2M带宽仅49元/香港VPS[主机]
- 江苏高防服务器哪里买?宿迁BGP、宿迁双线高防服务器的价格全球[VPS测评]
- UCloud全球大促活动:香港2核4G2M仅485元/年(台湾437元/美国VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-157538.html











