-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathip-addresses.php
86 lines (67 loc) · 1.9 KB
/
ip-addresses.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
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
<?php
/**
* Get the IP address used for public internet-facing applications.
*
* @author Dotan Cohen
* @version 2013-06-09
*
* @return string
*/
function get_public_ip_address()
{
// TODO: Add a fallback to http://httpbin.org/ip
// TODO: Add a fallback to http://169.254.169.254/latest/meta-data/public-ipv4
$url="simplesniff.com/ip";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* Get the IP address of the client accessing the website
*
* @author Dotan Cohen
* @version 2013-07-02
*
* @param null $return_type 'array', 'single'
*
* @return array|bool|mixed
*/
function get_user_ip_address($return_type=NULL)
{
// Consider: http://stackoverflow.com/questions/4581789/how-do-i-get-user-ip-address-in-django
// Consider: http://networkengineering.stackexchange.com/questions/2283/how-to-to-determine-if-an-address-is-a-public-ip-address
$ip_addresses = array();
$ip_elements = array(
'HTTP_X_FORWARDED_FOR', 'HTTP_FORWARDED_FOR',
'HTTP_X_FORWARDED', 'HTTP_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_CLUSTER_CLIENT_IP',
'HTTP_X_CLIENT_IP', 'HTTP_CLIENT_IP',
'REMOTE_ADDR'
);
foreach ( $ip_elements as $element ) {
if(isset($_SERVER[$element])) {
if ( !is_string($_SERVER[$element]) ) {
// Log the value somehow, to improve the script!
continue;
}
$address_list = explode(',', $_SERVER[$element]);
$address_list = array_map('trim', $address_list);
// Not using array_merge in order to preserve order
foreach ( $address_list as $x ) {
$ip_addresses[] = $x;
}
}
}
if ( count($ip_addresses)==0 ) {
return FALSE;
} elseif ( $return_type==='array' ) {
return $ip_addresses;
} elseif ( $return_type==='single' || $return_type===NULL ) {
return $ip_addresses[0];
}
}
?>