-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.php
More file actions
executable file
·46 lines (43 loc) · 1.27 KB
/
util.php
File metadata and controls
executable file
·46 lines (43 loc) · 1.27 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
<?php
/*
* get client side ip
* Notice: This method works best in the situation where the server is behind proxy
* and proxy will replace HTTP_CLIENT_IP. If your app is exposed straight to
* the Internet, this may return a wrong ip when a visits from Intranet but
* claims from Internet. It is a trade-off.
*/
function cr_get_client_ip()
{
$ip = $_SERVER['REMOTE_ADDR'];
// REMOTE_ADDR may not be real ip in case server is behind proxy (nginx, docker etc.)
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
for ($i = 0; $i < count($ips); $i++) {
if (!preg_match('/^(10│172.16│192.168)./i', $ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
}
return $ip;
}
/* get from $_GET */
function cr_get_GET($key, $default = null)
{
if (isset($_GET[$key]) && strlen($_GET[$key]) > 0) {
return $_GET[$key];
}
return $default;
}
/* get from $_POST */
function cr_get_POST($key, $default = null)
{
if (isset($_POST[$key]) && strlen($_POST[$key]) > 0) {
return $_POST[$key];
}
return $default;
}