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
ClientIpHelper
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
4
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
4
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmscitizenapi\Helper;
6
7class ClientIpHelper
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        foreach ($headers as $header) {
21            if (isset($_SERVER[$header])) {
22        // Handle comma-separated IPs (e.g. X-Forwarded-For)
23                $ips = array_map('trim', explode(',', $_SERVER[$header]));
24                $ip = $ips[0];
25                if (filter_var($ip, FILTER_VALIDATE_IP)) {
26                    return $ip;
27                }
28                return $ip;
29            }
30        }
31
32        return '127.0.0.1';
33    }
34}