本文实例为大家分享了php判断IP地址是否在多个IP段内的具体代码,供大家参考,具体内容如下
IP.class.php
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
<?php
class Ip {
/**
* 取IP
* @return string
*/
public static function get() {
if ($_SERVER['HTTP_CLIENT_IP'] && $_SERVER['HTTP_CLIENT_IP']!='unknown') {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ($_SERVER['HTTP_X_FORWARDED_FOR'] && $_SERVER['HTTP_X_FORWARDED_FOR']!='unknown') {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
/**
* IP转成整形数值
* @param string $ip IP
* @return int
*/
public static function ipToInt($ip) {
$ips = explode('.',$ip);
if (count($ips)==4) {
$int = $ips[0]*256*256*256+$ips[1]*256*256+$ips[2]*256+$ips[3]; //根据IP,a,b,c类进行计算
} else {
//throw new Exception('ip is error');
Tool::Alert('IP地址存在错误...'); //一个工具类,弹出提示信息
}
return $int;
}
/**
* 判断IP是否在一个IP段内
* @param string $startIp 开始IP
* @param string $endIp 结束IP
* @param string $ip IP
* @return bool
*/
public static function isIn($startIp, $endIp, $ip) {
$start = Ip::ipToInt($startIp);
$end = Ip::ipToInt($endIp);
$ipInt = Ip::ipToInt($ip);
$result = false;
if ($ipInt>=$start && $ipInt<=$end) {
$result = true;
}
return $result;
}
}
?>
|
IpRang.class.php
?
|
1
2
3
4
5
6
7
8
9
|
<?php
//将不同的IP段存储到数组中..
$iprang=array(
array('222.243.159.1','222.243.159.255'),
array('10.1.1.1','10.1.1.255')
);
?>
|
test.php
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?php
require_once 'Tool.class.php'; //工具类
require_once 'IP.class.php'; //IP类
require_once 'IpRang.class.php'; //IP段范围
$ip = IP::get(); //获取IP地址
$tag='1';
foreach($iprang as $key => $value){
if(!IP::isIn($value[0], $value[1], $ip)){
continue;
}else{
$tag.=$key;
}
}
if(mb_strlen($tag,'utf-8')==1){
echo "<script src='/iplookup/iplookup.php?format=js&ip=".$ip."' type='text/javascript'></script>";//调用新浪IP接口
echo "<script type='text/javascript'>alert('很遗憾,您所用的设备网络不在某某范围内...\\\\n".$ip."\\\\n'+remote_ip_info.province+remote_ip_info.city+remote_ip_info.district); $(\\"input[name='submit']\\").attr(\\"disabled\\",true);</script>";
//弹出提示框,显示IP地址、地址以及将提交按钮置为不可用状态
}
?>
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/luowei85520/article/details/77983868
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 23
-
2025-05-25 23
-
2025-05-27 55
-
2025-06-04 38
-
2025-05-25 100
热门评论

