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
- 便宜 $1.08 月 256M 内存 15G 硬盘 1.5T 流量@1G全球[VPS测评]
- GigsGigsCloud→$4.99 月 256MB 内存 15GB 虚拟空间(主机)
- HostKvm→$7.35 月 KVM-双核 4GB 30GB 1.5T美国VPS[主机]
- Online - 独立服务器限时特惠 Start-2-M-SSD 月付€独立服务器[U]
- OneVPS→日本东京完全可看美区 Netflix 耐飞视频 VPS N日本VPS[主机]
- WiredBlade→$5 月 KVM-2GB 20GB 1TB 凤凰城全球[VPS测评]
- stsdust 广州移动 vds 预售,1Gbps 带宽无限流量,硬件资全球[VPS测评]
- Redis、Opcache对WordPress实现内存级缓冲优化全球[VPS测评]
- 恒创科技元旦 香港/美国VPS 2折起;高防服务器6折优惠美国VPS[主机]
- 搬瓦工VPS补货DC9 CN2 GIA限量促销年付39.99美金套餐全球[VPS测评]
- 如何在百度搜索结果中显示你的WordPress站点Logo全球[VPS测评]
- 如何实现通过Gzip压缩实现WordPress站点加速的各种方法全球[VPS测评]
- dynadot国外域名注册商 .xyz域名首年注册仅8元,两年付52元;全球[VPS测评]
- 我爱水煮鱼WordPress SEO 宝典:怎么才能让你的博客流量增长1全球[VPS测评]
- 个人博客平台选择 Typecho 还是 WordPress ?全球[VPS测评]
- Wikihost CN2 香港虚拟主机 → 独立IP → 限量446元独立服务器[U]
- 跨境电商拔节生长关键期,宁波政协委员月谈会精准助力!全球[VPS测评]
- 很简单的图片不规则布局样式,纯css样式实现的图片瀑布流布局全球[VPS测评]
- 磐逸云怎么样?1核1G香港安畅CN2 VPS带宽5M年付128元香港VPS[主机]
- 购买香港服务器需要做哪些准备香港VPS[主机]
- 六一云:香港CMI VPS,10G防御,1核/1G/40G SSD/15香港VPS[主机]
- 麻花云双12活动:香港CN2云主机首月9元;安徽移动vps月付29元香港VPS[主机]
- 深圳市城市交通规划设计研究中心股份有限公司关于与腾讯云计算(北京)有限责全球[VPS测评]
- 搬瓦工:美国香港cn2 gia线路VPS,10Gbps带宽,移动+联通企美国VPS[主机]
- TmhHost优惠码:黑五优惠全场8折,洛杉矶vps、日本CN2、韩国C日本VPS[主机]
- 茶猫云:香港云服务器,支持windows,2M带宽,34元/月WINDOWS
- 天生云:香港沙田CN2 GIA VPS,1核1G40G/2M带宽,月付8香港VPS[主机]
- 极光KVM:1核512M/50Mbps/BGP+CN2/美西vps/年付香港VPS[主机]
- 瓜云互联怎么样?香港/美国洛杉矶CN2高防vps 50G月付34元美国VPS[主机]
- ucloud云服务器全球购:香港/韩国/日本/台湾/越南/泰国服务器最低日本VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-214198.html











