0

How can I get the Machine IP of visitors of my website logged.

I've used

function getIp(){

$ip = $_SERVER['REMOTE_ADDR'];
if($ip){
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
return $ip;
}

BUt what I get is my server IP.

Thanks

3
Contributors
2
Replies
24
Views
2 Years
Discussion Span
Last Post by matrixdevuk
0
function getIp(){ 

    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } 
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } 
    else{
        $ip = $_SERVER['REMOTE_ADDR'] ? $_SERVER['REMOTE_ADDR'] : "";
    }
    return $ip; 
}
0

@Masterblank That could be simplified a lot and improved.

function getIp($ifNull = "") { # allow specification of null value
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        return $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        return $_SERVER['HTTP_X_FORWARDED_FOR'];
    }

    return $_SERVER['REMOTE_ADDR'] ? $_SERVER['REMOTE_ADDR'] : $ifNull;
}

getIp("127.0.0.1");
>> IP or 127.0.0.1 if it can't find it
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.