Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Get Client IP Address

I've tried to get the IP address of client browsing the site using $_SERVER['REMOTE_ADDR'], but I'm not getting the exact IP of the client please help me... Thanks

like image 326
user389055 Avatar asked Dec 28 '22 08:12

user389055


2 Answers

$_SERVER['REMOTE_ADDR'] 

is the best you will get really.

There are various other headers that can be sent by the client (HTTP_FORWARDED_FOR et al.) - see this question for a complete overview - but these can be freely manipulated by the client and are not to be deemed reliable.

like image 198
Pekka Avatar answered Feb 04 '23 08:02

Pekka


// Get the real client IP

function getIP() {
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
        $ip = getenv("HTTP_CLIENT_IP");
    } else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    } else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
        $ip = getenv("REMOTE_ADDR");
    } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
        $ip = $_SERVER['REMOTE_ADDR'];
    } else {
        $ip = "unknown";
    }
    return($ip);
}
like image 20
xhlwill Avatar answered Feb 04 '23 07:02

xhlwill