最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

nonce - wp_verfy_nonce keeps giving false

programmeradmin1浏览0评论

i have a form somthing like this

<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>

and then i have a ajax for this like so

$('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;
});

and then in my php side to handle the ajax m doing this

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');

the wp_verify_nonce() function (in built WordPress function)

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;
    }

so when i do this i get false i have checked if the value $_POST['nonce_sample_doc'], goes to the function and it does i have also checked that if it goes inside the wp_verify_nonce() function and it does what i found odd was that

in side the wp_verify_nonce() function when i checked the value of $expected and $nonce it was different

so my question is how to make the value true inside my sample_doc function

发布评论

评论列表(0)

  1. 暂无评论