WordPress real IP address fix

WordPress could be behind reverse proxy and $_SERVER['REMOTE_ADDR'] variable could show wrong IP address.

Add this code to wp-config.php to fix wrong IP:


if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { //check ip from share internet

	$ip = $_SERVER['HTTP_CLIENT_IP'];

} elseif ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { // to check ip is pass from proxy, also could be used ['HTTP_X_REAL_IP ']

	$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

} else {

	$ip = $_SERVER['REMOTE_ADDR'];

}



$_SERVER['REMOTE_ADDR'] = $ip; // set ip via complex method because remote_addr could have proxy ip

Article about getting real IP address.

Leave a Comment