php: get IP address even if client is behind proxy



if (getenv("HTTP_CLIENT_IP")) {
    $ip = getenv("HTTP_CLIENT_IP");
} elseif (getenv("HTTP_X_FORWARDED_FOR")) {
    $ip = getenv("HTTP_X_FORWARDED_FOR");
} elseif (getenv("REMOTE_ADDR")) {
    $ip = getenv("REMOTE_ADDR");
}
or function:
function getIp () {
    if (getenv('HTTP_CLIENT_IP')) {       $ip['HTTP_CLIENT_IP']       = getenv('HTTP_CLIENT_IP'); }
    if (getenv('HTTP_X_FORWARDED_FOR')) { $ip['HTTP_X_FORWARDED_FOR'] = getenv('HTTP_X_FORWARDED_FOR'); }
    if (getenv('REMOTE_ADDR')) {          $ip['REMOTE_ADDR']          = getenv('REMOTE_ADDR'); }
    if (getenv('HTTP_USER_AGENT')) {      $ip['HTTP_USER_AGENT']      = getenv('HTTP_USER_AGENT'); }

    return $ip;
}
Last update:
2010-03-24 09:03
Author:
Daniel
Revision:
1.2
Average rating:0 (0 Votes)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.