How to get client ip with php behind Load Balancer Server

The code to find the correct IP address is quite simple:

if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and $_SERVER['HTTP_X_FORWARDED_FOR']) {
$clientIpAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$clientIpAddress = $_SERVER['REMOTE_ADDR'];
}