Lines 95.00% 19 / 20
Functions and Methods 0.00% 0 / 1
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ClientIp 95.00% 19 / 20 0.00% 0 / 1 7 0.00% 0 / 1
 getClientIp 95.00% 19 / 20 0.00% 0 / 1 7
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            $headerValue = $_SERVER[$header];
27            if (!is_string($headerValue) || $headerValue === '') {
28                continue;
29            }
30
31            $ips = array_map('trim', explode(',', $headerValue));
32            foreach ($ips as $ip) {
33                if (filter_var($ip, FILTER_VALIDATE_IP)) {
34                    return $ip;
35                }
36            }
37        }
38
39        return '127.0.0.1';
40    }
41}