-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathip_area_test.php
More file actions
111 lines (99 loc) · 1.96 KB
/
ip_area_test.php
File metadata and controls
111 lines (99 loc) · 1.96 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/** 引入IP段 */
$aryIps = require( 'result.php' );
/**
* 获得用户的真实IP地址
*
* @return string
*/
function realIp()
{
if (isset($_SERVER))
{
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
/* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
foreach ($arr AS $ip)
{
$ip = trim($ip);
if ($ip != 'unknown')
{
$realip = $ip;
break;
}
}
}
elseif (isset($_SERVER['HTTP_CLIENT_IP']))
{
$realip = $_SERVER['HTTP_CLIENT_IP'];
}
else
{
if (isset($_SERVER['REMOTE_ADDR']))
{
$realip = $_SERVER['REMOTE_ADDR'];
}
else
{
$realip = '0.0.0.0';
}
}
}
else
{
if (getenv('HTTP_X_FORWARDED_FOR'))
{
$realip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_CLIENT_IP'))
{
$realip = getenv('HTTP_CLIENT_IP');
}
else
{
$realip = getenv('REMOTE_ADDR');
}
}
preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
$realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
return $realip;
}
/**
* 是否需要拒绝
*
* @param array $_aryIps 拒绝的IP列表
* @param string $_strIp IP地址
*
* @return boolean
*/
function isNeedReject( $_aryIps = array() , $_strIp )
{
// 判断是否是合法IP
preg_match( '/\d+\.\d+\.\d+\.\d+/' , $_strIp , $res );
if ( empty( $res[0] ) )
return true;
// 转换IP
$tmp = explode( '.' , $_strIp );
$tmps = '';
foreach ( $tmp as $t )
$tmps .= sprintf("%03d", $t);
// 转换后的IP值
$intIp = intval( $tmps );
foreach ( $_aryIps as $ips )
{
// 如果在某一个IP范围内,则不允许通过
if ( $intIp >= $ips[0] && $intIp <= $ips[1] )
return true;
}
return false;
}
/** 获得IP */
$ip = realIp();
echo "Your ip address is: {$ip}\n";
/** 判断是否在拒绝的范围内 */
if ( isNeedReject( $aryIps , $ip ) === true )
echo "Your ip is in the china, reject!\n";
else
echo "Congritulations! You have a good ip!\n";
?>