ajax die,check_ajax_referer()

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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值