1. 首页
  2. WordPress钩子手册

check_ajax_referer

do_action( ‘check_ajax_referer’, string $action, false|int $result )

动作钩子::一旦Ajax请求被验证或没有验证,就会触发。
Action Hook: Fires once the Ajax request has been validated or not.

目录锚点:#参数#源码


参数(Parameters)

参数 类型 说明
$action (string) Ajax nonce操作。
$result (false | int) 如果nonce无效,则为False;如果nonce有效并在0-12小时前生成,则为1;如果nonce有效并在12-24小时前生成,则为2。

源码(Source)

/**
 * Verifies the AJAX request to prevent processing requests external of the blog.
 *
 * @since 2.0.3
 *
 * @param int|string   $action    Action nonce.
 * @param false|string $query_arg Optional. Key to check for the nonce in `$_REQUEST` (since 2.5). If false,
 *                                `$_REQUEST` values will be evaluated for '_ajax_nonce', and '_wpnonce'
 *                                (in that order). Default false.
 * @param bool         $die       Optional. Whether to die early when the nonce cannot be verified.
 *                                Default true.
 * @return false|int False if the nonce is invalid, 1 if the nonce is valid and generated between
 *                   0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
 */
function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
	$nonce = '';

	if ( $query_arg && isset( $_REQUEST[ $query_arg ] ) )
		$nonce = $_REQUEST[ $query_arg ];
	elseif ( isset( $_REQUEST['_ajax_nonce'] ) )
		$nonce = $_REQUEST['_ajax_nonce'];
	elseif ( isset( $_REQUEST['_wpnonce'] ) )
		$nonce = $_REQUEST['_wpnonce'];

	$result = wp_verify_nonce( $nonce, $action );

	if ( $die && false === $result ) {
		if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
			wp_die( -1 );
		else
			die( '-1' );
	}

	/**
	 * Fires once the AJAX request has been validated or not.
	 *
	 * @since 2.1.0
	 *
	 * @param string    $action The AJAX nonce action.
	 * @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between
	 *                          0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
	 */
	do_action( 'check_ajax_referer', $action, $result );

	return $result;
}
endif;

if ( !function_exists('wp_redirect') ) :
更新版本 源码位置 使用 被使用
2.1.0 wp-includes/pluggable.php:1189 1 0

check_ajax_referer 为WP2原创文章,链接:https://www.wp2.cn/hook/check_ajax_referer/

发表评论

您的电子邮箱地址不会被公开。