WordPress website uses CDN to obtain visitors’ real IP

WordPress seems to use REMOTE_ADDR when transferring the IP of the data inventory, so that the comment information in the database will all be IPs from various CDN servers.

Add the following code to the wp-config.php file to obtain the real IP of the visitor behind the CDN. The core of this function is to replace REMOTE_ADDR with the parsed HTTP_X_FORWARDED_FOR. 

// WordPress 使用 CDN 后获取访客真实 IP
if( !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
    $get_HTTP_X_FORWARDED_FOR = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    $_SERVER['REMOTE_ADDR'] = trim($get_HTTP_X_FORWARDED_FOR[0]);
}

Guess you like

Origin blog.csdn.net/qq_39339179/article/details/132553764