wp_verfy_nonce不断给假

时间:2019-06-12 08:38:31

标签: wordpress nonce

我有这样的东西

<form id="sample_docs" method="post" action="<?php echo admin_url('admin-ajax.php'); ?>">
 <?php wp_nonce_field('nonce_action_sample_docs', 'nonce_sample_docs'); ?>
 <input type="hidden" name="action" value="sample_docs">
 </form>

然后我为此使用了一个ajax

$('form#sample_docs').on('submit', function(e){
    e.preventDefault();
    id = $(this).attr('id');
    url = $(this).attr('action');
    $.ajax({
        url: url,
        type: "POST",
        data: formData,
        cache: false,
        processData: false,
        contentType: false,
        async: true,
        headers: {
             "cache-control": "no-cache"
           },
        success: function(data){
            data = $.parseJSON(data);
            console.log('data', data);

        }
    });
    return false;
});

然后在我的php端处理ajax m

function sample_docs()
{

 var_dump(wp_verify_nonce($_POST['nonce_sample_docs'],'nonce_action_sample_docs));
 die();
if(wp_verify_nonce($_POST['nonce_sample_docs'],'nonce_action_sample_docs'))
  {
    //do something
   }
} add_action('wp_ajax_sample_docs', 'sample_docs'); add_action('wp_ajax_nopriv_sample_docs', 'sample_docs');

wp_verify_nonce()函数(内置WordPress函数)

function wp_verify_nonce( $nonce, $action = -1 ) {
        $nonce = (string) $nonce;
        $user  = wp_get_current_user();
        $uid   = (int) $user->ID;
        if ( ! $uid ) {
            /**
             * Filters whether the user who generated the nonce is logged out.
             *
             * @since 3.5.0
             *
             * @param int    $uid    ID of the nonce-owning user.
             * @param string $action The nonce action.
             */
            $uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
        }

        if ( empty( $nonce ) ) {
            return false;
        }

        $token = wp_get_session_token();
        $i     = wp_nonce_tick();

        // Nonce generated 0-12 hours ago
        $expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
        if ( hash_equals( $expected, $nonce ) ) {
            return 1;
        }

        // Nonce generated 12-24 hours ago
        $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
        if ( hash_equals( $expected, $nonce ) ) {
            return 2;
        }

        /**
         * Fires when nonce verification fails.
         *
         * @since 4.4.0
         *
         * @param string     $nonce  The invalid nonce.
         * @param string|int $action The nonce action.
         * @param WP_User    $user   The current user object.
         * @param string     $token  The user's session token.
         */
        do_action( 'wp_verify_nonce_failed', $nonce, $action, $user, $token );

        // Invalid nonce
        return false;
    }

所以当我这样做时,我得到false 我检查了值$_POST['nonce_sample_doc'],是否用于函数 而且我是否也检查过它是否在wp_verify_nonce()函数中,并且确实发现了那个错误

wp_verify_nonce()函数旁边,当我检查$expected$nonce的值时,它是不同的

所以我的问题是如何使值成为真 在我的sample_doc函数中

1 个答案:

答案 0 :(得分:1)

您的sample_doc函数中有一个错字,输入名称为nonce_sample_docs,但是您在$ _POST中使用了nonce_sample_doc

固定代码:

function sample_doc()
{

 var_dump(wp_verify_nonce($_POST['nonce_sample_docs'],'nonce_action_sample_docs));
 die();
if(wp_verify_nonce($_POST['nonce_sample_doc'],'nonce_action_sample_docs'))
  {
    //do something
   }
} add_action('wp_ajax_sample_doc', 'sample_doc'); add_action('wp_ajax_nopriv_sample_doc', 'sample_doc');