Get visitors IP in php

<?php

    public function get_client_ip()
    {
        $header_checks = array(
            'HTTP_CLIENT_IP',
            'HTTP_PRAGMA',
            'HTTP_XONNECTION',
            'HTTP_CACHE_INFO',
            'HTTP_XPROXY',
            'HTTP_PROXY',
            'HTTP_PROXY_CONNECTION',
            'HTTP_VIA',
            'HTTP_X_COMING_FROM',
            'HTTP_COMING_FROM',
            'HTTP_X_FORWARDED_FOR',
            'HTTP_X_FORWARDED',
            'HTTP_X_CLUSTER_CLIENT_IP',
            'HTTP_FORWARDED_FOR',
            'HTTP_FORWARDED',
            'ZHTTP_CACHE_CONTROL',
            'REMOTE_ADDR'
        );

        foreach ($header_checks as $key) {
            if (array_key_exists($key, $_SERVER) === true) {
                foreach (explode(',', $_SERVER[$key]) as $ip) {
                    $ip = trim($ip);

                    //filter the ip with filter functions
                    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
                        return $ip;
                    }
                }
            }
        }
    }

 

Top