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
- 凯特云→21 元 月 512MB 内存 10GB SSD 空间 500G虚拟空间(主机)
- 大盘机 $2.99 月 2 核 CPU 512M 内存 250G 硬盘 全球[VPS测评]
- laravel 框架提示→TokenMismatchException 全球[VPS测评]
- JS 计算两个时间戳相差月数 天数 时数 分钟 秒数全球[VPS测评]
- MoeCloud→900 元 月 2 核 4GB 内存 20GB SSD香港VPS[主机]
- php 判断数组是否有重复的值全球[VPS测评]
- 国外 VPS – 支付宝 微信付款全球[VPS测评]
- 便宜 VPS $0.99 月 256M 内存 5G SSD 1Gbps 全球[VPS测评]
- KT 旗下 iON 品牌上线路新加坡机房 预够进行中 可选 CN2 2G全球[VPS测评]
- apache 设置 txt 文件直接下载而不是网页在线打开全球[VPS测评]
- 微信怎么开启 关闭“附近的人”功能?全球[VPS测评]
- 线路不行-HostDoc→£4.99 月 8GB 内存 30GB SSD虚拟空间(主机)
- 一个望一个张是什么成语?全球[VPS测评]
- WordPress代码实现用户注册时验证邮箱真实性全球[VPS测评]
- 六六云新上洛杉矶cera联通AS9929线路VPS,100M大带宽,延迟全球[VPS测评]
- Cloud9 ge-月付27$ 格鲁吉亚 2核2G内存16G硬盘 不限流全球[VPS测评]
- CrownCloud -512M内存 1000G HDD 6T流量 1G全球[VPS测评]
- Mansora 2018圣诞优惠全球[VPS测评]
- 40火伞高压电爬坡更让企业用电于民全球[VPS测评]
- kvmla,新加坡VPS、香港VPS、日本VPS,2核2G云主机仅425日本VPS[主机]
- 咖啡主机:美国高防vps、香港vps 可享受6-7折促销 每月仅需14元美国VPS[主机]
- CMIVPS:香港vps/2核/2GB/20GB空间/不限流量/KVM/虚拟空间(主机)
- 中概股扫描网传拼多多正筹备跨境电商平台首站登陆美国美国VPS[主机]
- 2.45亿、成都公安(大数据基座-服务器)采购:神码、虹信、海康、超聚变全球[VPS测评]
- 硅云服务器拼团活动:香港云服务器1核1G,拼团价仅98元/年香港VPS[主机]
- 酷锐云:春节特惠,4核4G80G10M仅60元/月起,香港/美国云服务器美国VPS[主机]
- 无忧云怎么样?大连BGP德阳100G高防香港CN2VPS云服务器香港VPS[主机]
- 者也IO,香港日本美国云服务器促销,2核2G1000元/2年,7折+内存日本VPS[主机]
- 咖啡主机:香港云服务器15.6元/月(季付);美西1G云服务器16.8元香港VPS[主机]
- Weebly网站迁移到WordPress的实操上手。全球[VPS测评]
转载请注明原文地址:http://140.238.13.167:12355/read-217190.html











