Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ClientIp
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
1 / 1
 getClientIp
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Slim\Helper;
6
7class ClientIp
8{
9    public static function getClientIp(): string
10    {
11        $headers = [
12            'HTTP_CLIENT_IP',
13            'HTTP_X_FORWARDED_FOR',
14            'HTTP_X_FORWARDED',
15            'HTTP_X_CLUSTER_CLIENT_IP',
16            'HTTP_FORWARDED_FOR',
17            'HTTP_FORWARDED',
18            'REMOTE_ADDR',
19        ];
20
21        foreach ($headers as $header) {
22            if (!isset($_SERVER[$header])) {
23                continue;
24            }
25
26            $ips = array_map('trim', explode(',', (string) $_SERVER[$header]));
27            foreach ($ips as $ip) {
28                if (filter_var($ip, FILTER_VALIDATE_IP)) {
29                    return $ip;
30                }
31            }
32        }
33
34        return '127.0.0.1';
35    }
36}