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
- Gestion DBI→加拿大虚拟主机 2GB NVMe 空间 不限流量虚拟空间(主机)
- mysql ERROR 1017:Can't find file: ' 全球[VPS测评]
- DediPath VPS 调整升级,G 口带宽无限流量,洛杉矶 Inte全球[VPS测评]
- CloudCone→洛杉矶 KVM 年费 12.5 美元起 按小时计费随全球[VPS测评]
- [11.11]恒创科技香港云服务器 独立服务器 4 折起 充值最高返 1独立服务器[U]
- 新年促销 reprisehosting→超级便宜的美国西雅图独立服务器独立服务器[U]
- Vultr 推出 Object Storage 对象存储 250G 空间虚拟空间(主机)
- php 生成服务器信息函数全球[VPS测评]
- 2019 年 Virmach 低价 VPS 购买及优惠码使用教程全解全球[VPS测评]
- DedeTag Engine Create File False 错误 全球[VPS测评]
- 大硬盘 VPS 70 每月 1G 内存 220G 硬盘 1.5T 流量全球[VPS测评]
- RAKsmart→日本站群服务器 50M 带宽 258IP 每月 241站群服务器[IP]
- 使用WooRank SEO分析工具来优化的你的博客全球[VPS测评]
- PerfectQualityHosting提供乌克兰/拉脱维亚/摩尔多瓦全球[VPS测评]
- RackNerd 三款AMD Ryzen便宜VPS限时促销,年付$18.全球[VPS测评]
- shockhosting-日本 1C2G30GB 1Gbps 1TB 4日本VPS[主机]
- anyhk香港HKT商宽NAT VPS,1Gbps无限流量,终身8折¥3香港VPS[主机]
- 阿里行癫:如何理解当下的云计算全球[VPS测评]
- 初忆云:香港美国cn2云服务器30元;香港cn2高防vps30元,美国1美国VPS[主机]
- 恒创科技:香港免备案CN2线路VPS/美国CN2直连VPS,月付28元起美国VPS[主机]
- HostKvm,香港云地VPS,全场八折,三网直连,电信CN2,2核4G香港VPS[主机]
- 六一云:香港CN2/洛杉矶高防85折优惠,CDN半年优惠,充值返利送水杯香港VPS[主机]
- 奇异互动,2核4G香港云服务器仅655元/年,注册领550元新手大礼包香港VPS[主机]
- 蓝米云:美国cera vps/香港cn2vps套餐,1核2G/40G/1美国VPS[主机]
- 好朋友51WORLD启动“地球克隆计划5”,我要去元宇宙参加了全球[VPS测评]
- 跨境电商拔节生长关键期,宁波政协委员月谈会精准助力!全球[VPS测评]
- 极光KVM:新上香港CN2VPS,终身6折优惠,1核/1G/5Mbps,香港VPS[主机]
- HostHatch:1核/2GB/20GB空间/KVM/香港/洛杉矶/纽虚拟空间(主机)
- ucloud香港服务器,香港云服务器vps主机优惠价格香港VPS[主机]
- 野草云:香港CN2VPS年付36折,137元/年;独立服务器350元月起独立服务器[U]
转载请注明原文地址:http://140.238.13.167:12355/read-211226.html











