首页 > 解决方案 > 使用 wp_localize_script 将用户 ID 传递给 JavaScript 是否安全?

问题描述

在收到 AJAX 发布请求后,我正在尝试在后端 PHP 上恢复 WordPress 用户的 ID。

我无法在后端的 AJAX 处理程序函数中使用 wp_get_current_user()(我不完全确定为什么会失败)

我的解决方案是使用 wp_localize_script 将 wp_get_current_user()->ID 传递给浏览器。我担心用户可能能够在他们的浏览器中更改用户 ID 并冒充另一个用户。这可能吗?如果是这样,是否有更好的方法在 ajax POST 中获取用户 ID?

以下是我当前的设置:

JS 阿贾克斯

var fd = new FormData();
fd.append('user_id', ajax_object.user_id);
fd.append('action', 'tb_upload');

jQuery.ajax({
        type: "POST",
        async: false,
        url: ajax_object.ajaxurl,
        data: fd,
        contentType: false,
        processData: false,

        success: function (resp) {
            console.log("in success!");
            if (resp != null) {
                console.log(resp.data);
            } else {
                console.log("Error: Response is null!");
            }
        }
    });

标签: javascriptajaxwordpress

解决方案


推荐阅读