gpt4 book ai didi

ajax - Wordpress nonce 检查总是错误的

转载 作者:行者123 更新时间:2023-12-02 04:06:27 24 4
gpt4 key购买 nike

我在通过 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 操作。这会将接收到的 nonce 字段附加到响应中...
(回到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 {"success":false,"debugNonce":"c4817b947a"}

第一个 c4817b947a 是因为来自 nonce 生成函数的回声。它不会影响验证发生的方式。

我的结论是 wp_verify_nonce 总是失败。

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

最佳答案

我刚刚遇到了 similar issue事实证明,我所要做的就是以管理员身份重新登录。我认为它也适用于您的情况,因为提供的代码中的其他所有内容似乎都很好。

我想这与 Wordpress 中 session 的处理方式有关。

关于ajax - Wordpress nonce 检查总是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7375058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com