WordPress behind Google PageSpeed get real client IP

I am using Google PageSpeed service as CDN. So PageSpeed server forwards client requests to my WordPress server, with the client IP address set as PageSpeed server.

However, real client IP is also forwarded in the HEADER – “HTTP_X_FORWARDED_FOR”. Here’s the short code for wp-config.php to exact and set the real IP.

if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$xffaddrs = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = $xffaddrs[0];
}