Thursday, June 14, 2012

PHP: How to detect / get the real client IP address of website visitors


echo $_SERVER['REMOTE_ADDR'];
function get_ip_address() {
    foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
        if (array_key_exists($key, $_SERVER) === true) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
                if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                    return $ip;
                }
            }
        }
    }
}
Server variables


GATEWAY_INTERFACE = $_SERVER['GATEWAY_INTERFACE'];
SERVER_ADDR = $_SERVER['SERVER_ADDR'];
SERVER_NAME = $_SERVER['SERVER_NAME'];
SERVER_SOFTWARE = $_SERVER['SERVER_SOFTWARE'];
SERVER_PROTOCOL = $_SERVER['SERVER_PROTOCOL'];
REQUEST_METHOD = $_SERVER['REQUEST_METHOD'];
REQUEST_TIME = $_SERVER['REQUEST_TIME'];
QUERY_STRING = $_SERVER['QUERY_STRING'];
DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
HTTP_ACCEPT = $_SERVER['HTTP_ACCEPT'];
HTTP_ACCEPT_CHARSET = $_SERVER['HTTP_ACCEPT_CHARSET'];
HTTP_ACCEPT_ENCODING = $_SERVER['HTTP_ACCEPT_ENCODING'];
HTTP_ACCEPT_LANGUAGE = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
HTTP_CONNECTION = $_SERVER['HTTP_CONNECTION'];
HTTP_HOST = $_SERVER['HTTP_HOST'];
HTTP_REFERER = $_SERVER['HTTP_REFERER'];
HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
HTTPS = $_SERVER['HTTPS'];
REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
REMOTE_HOST = $_SERVER['REMOTE_HOST'];
REMOTE_PORT = $_SERVER['REMOTE_PORT'];
SCRIPT_FILENAME = $_SERVER['SCRIPT_FILENAME'];
SERVER_ADMIN = $_SERVER['SERVER_ADMIN'];
SERVER_PORT = $_SERVER['SERVER_PORT'];
SERVER_SIGNATURE = $_SERVER['SERVER_SIGNATURE'];
PATH_TRANSLATED = $_SERVER['PATH_TRANSLATED'];
SCRIPT_NAME = $_SERVER['SCRIPT_NAME'];
REQUEST_URI = $_SERVER['REQUEST_URI'];
PHP_AUTH_DIGEST = $_SERVER['PHP_AUTH_DIGEST'];
PHP_AUTH_USER = $_SERVER['PHP_AUTH_USER'];
PHP_AUTH_PW = $_SERVER['PHP_AUTH_PW'];
AUTH_TYPE = $_SERVER['AUTH_TYPE'];

No comments:

Post a Comment

There was an error in this gadget