check_ajax_referer( int|string $action = -1, false|string $query_arg = false, bool $die = true )
验证Ajax请求以防止处理博客外部的请求。
Verifies the Ajax request to prevent processing requests external of the blog.
参数(Parameters)
参数
类型
必填
说明
$action
(int | string)
可选
立即行动。
$query_arg
(false | string)
可选
键检查$u请求中的nonce(从2.5开始)。如果为false,$u请求值将被计算为’uAjax’u nonce’和’uWPnonce’(按该顺序)。
$die
(bool)
可选
当暂时性无法验证时是否提前死亡。
返回(Return)
(int|false)1如果nonce有效并在0-12小时前生成,2如果nonce有效并在12-24小时前生成。如果nonce无效,则为False。
源码(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.0.3
wp-includes/pluggable.php:1163
79
7
笔记(Notes)
例子
check_ajax_referer() 为WP2原创文章,链接:https://www.wp2.cn/functions/check_ajax_referer-2/