Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In some cases that router translate/convert the client information when forward all the packet to the server, PHP does not return correct client IP address by $_SERVER['REMOTE_ADDR']. In that case, the original client information is saved in HTTP_X_FORWARDED_FOR. Below code shows how to get the correct client IP address in PHP.

Code Block
$ipAddress = $_SERVER['REMOTE_ADDR'];
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
    $ipAddress = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
}