pre_comment_user_ip

最后更新于:2021-11-27 18:46:00

apply_filters( ‘pre_comment_user_ip’, string $comment_author_ip )

Filters the comment author’s IP address before it is set.

参数

$comment_author_ip

(string)
The comment author’s IP address.

源文件

文件: gc-includes/comment.php

View on Trac

add_filter( 'pre_comment_user_ip', 'auto_reverse_proxy_pre_comment_user_ip');

function auto_reverse_proxy_pre_comment_user_ip()
{    
	$REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];

	if (!empty($_SERVER['X_FORWARDED_FOR'])) {
		$X_FORWARDED_FOR = explode(',', $_SERVER['X_FORWARDED_FOR']);
		if (!empty($X_FORWARDED_FOR)) {
			$REMOTE_ADDR = trim($X_FORWARDED_FOR[0]);
		}
	}

	/*
	* Some PHP environments will use the $_SERVER['HTTP_X_FORWARDED_FOR'] 
	* variable to capture visitor address information.
	*/
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
		$HTTP_X_FORWARDED_FOR= explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
		if (!empty($HTTP_X_FORWARDED_FOR)) {
			$REMOTE_ADDR = trim($HTTP_X_FORWARDED_FOR[0]);
		}
	}

	return preg_replace('/[^0-9a-f:., ]/si', '', $REMOTE_ADDR);
}