我在通过ajax请求进行基本随机数验证时遇到问题。

这些是我的脚本加载器和CSS加载器功能:
(在gallery.php中)

function gallery_js_loader()
{
    if (!is_admin()) return;
    // async flash uploader
    wp_enqueue_script('swfobject', THEMEURL . "/lib/uploadify/swfobject.js", array(), false, true);
    wp_enqueue_script('uploadify', THEMEURL . "/lib/uploadify/jquery.uploadify.v2.1.4.min.js", array('jquery'), false, true);
    wp_enqueue_script('gallery_admin_scripts', THEMEURL . "/inc/galleries/gallery_admin_scripts.js", array(), false, true);
    wp_localize_script('gallery_admin_scripts', 'param',
                   array(
                        'basename' => GALLERYPOST,
                        'baselocation' => THEMEURL,
                        'nonce' => wp_create_nonce('file-upload-nonce'),
                        'thumb_width' => intval(get_option('thumbnail_size_w')),
                        'thumb_height' => intval(get_option('thumbnail_size_h'))
                   ));
// main styles


}

function gallery_css_loader()
{
    wp_enqueue_style('uploadify_styles', THEMEURL . "/lib/uploadify/uploadify.css");
    wp_enqueue_style('gallery_admin_styles', THEMEURL . "/inc/galleries/gallery_admin_styles.css");
}

 add_action('admin_print_scripts-post.php', 'gallery_js_loader');
 add_action('admin_print_scripts-post-new.php', 'gallery_js_loader');
 add_action('admin_print_styles-post.php', 'gallery_css_loader');
 add_action('admin_print_styles-post-new.php', 'gallery_css_loader');


 function gallery_upload_image()
 {
     $nonce = $_POST["nonce"];

     if (is_admin() && !empty($_FILES) /*&& wp_verify_nonce($nonce, 'file-upload-nonce')*/) {
         require_once(ABSPATH . 'wp-admin/includes/image.php');

         $tempFile = $_FILES['Filedata']['tmp_name'];
         //        $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
         $targetDir = wp_upload_dir(date('Y'));
         $targetFile = $targetDir['path'] . '/' . $_FILES['Filedata']['name'];
         $targetFile = str_replace(" ", "", $targetFile);

         move_uploaded_file($tempFile, $targetFile);

         $wp_filetype = wp_check_filetype(basename($targetFile), null);

         $attachment = array(
             'post_mime_type' => $wp_filetype['type'],
             'post_title' => preg_replace('/\.[^.]+$/', '', basename($targetFile)),
             'post_content' => '',
             'post_status' => 'inherit'
         );
         $result['attachmet_id'] = $attach_id = wp_insert_attachment($attachment, $targetFile);
         $result['recieved_nonce'] = $nonce;

         $attach_data = wp_generate_attachment_metadata($attach_id, $targetFile);
         wp_update_attachment_metadata($attach_id, $attach_data);

         $result['success'] = true;
     } else {
         $result['success'] = false;
         $result['recieved_nounce'] = $nonce;
         $result['error'] = array(
             'message' => 'No files or you are not admin ' . $nonce,
             'code' => 'E01'
         );
     }

     echo json_encode($result);
     exit;
 }

 add_action('wp_ajax_do_upload', 'gallery_upload_image');

在我的javascrtip文件中:
(在gallery.js中)
console.debug("Nonce received ",param.nonce); //c4817b947a

我的ajax调用将从php访问do_upload操作。这会将收到的现时字段附加到响应中...
(返回gallery.php)
function gallery_upload_image()
{
    $nonce = $_POST["nonce"];

    if ( wp_verify_nonce($nonce, 'file-upload-nonce')) {
        /* some logic here, nothing to do with nonce */
        $result['success'] = true;
        $result['debugNonce'] = $nonce;
    } // end validation
    else {
       //invalid nonce
       $result['success'] = false;
       $result['debugNonce'] = $nonce;
    }
}

收到的结果如下所示:
c4817b947a {“成功”:false,“debugNonce”:“c4817b947a”}

第一个c4817b947a是由于现时函数产生的回波。它不影响验证发生的方式。

我的结论是wp_verify_nonce总是失败。

我在本地主机上使用wp 3.2.1,全新安装,没有插件。

最佳答案

我刚遇到similar issue,事实证明,我要做的就是以admin身份重新登录。我认为它也应该适合您的情况,因为提供的代码中的所有其他内容似乎都还不错。

我想这与如何在Wordpress中处理 session 有关。

07-26 04:45