Conditions | 6 |
Paths | 9 |
Total Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
7 | public static function getClientIp() |
||
|
|||
8 | { |
||
9 | $ip_keys = [ |
||
10 | 'HTTP_CLIENT_IP', |
||
11 | 'REMOTE_ADDR', |
||
12 | 'HTTP_X_FORWARDED_FOR', |
||
13 | 'HTTP_X_FORWARDED', |
||
14 | 'HTTP_X_CLUSTER_CLIENT_IP', |
||
15 | 'HTTP_FORWARDED_FOR', |
||
16 | 'HTTP_FORWARDED', |
||
17 | ]; |
||
18 | foreach ($ip_keys as $key) { |
||
19 | if (array_key_exists($key, $_SERVER) === true) { |
||
20 | foreach (explode(',', $_SERVER[$key]) as $ip) { |
||
21 | // trim for safety measures |
||
22 | $ip = trim($ip); |
||
23 | // attempt to validate IP |
||
24 | if (self::validateIp($ip)) { |
||
25 | return $ip; |
||
26 | } |
||
27 | } |
||
28 | } |
||
29 | } |
||
30 | |||
31 | return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false; |
||
32 | } |
||
33 | |||
47 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: