javascript - 使用 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!");
}
}
});
解决方案
推荐阅读
- css - 一些(不是全部)后备 CSS 属性消失(create-react-app)
- python - 如何提取小数
- r - data.table 中的 Newey West 调整
- google-chrome - 如何为 Chrome 扩展禁用(灰显)页面操作?
- c++ - 什么可能导致 std::condition_variable notify_all 非常慢(> 2ms)?
- r - 尝试从网站读取 excel 文件时出错
- python - numba 并行 njit 编译不适用于 np.isnan()
- asp.net-core - 尝试使用 protobuf-net 通过 gRPC-Web 进行 Azure AD 身份验证
- double-click - 如何从 Konva 的 'dblclick' 事件中获取 Konva 标签的 ID
- javascript - 如何使用 jest 模拟原始 const 值并恢复模拟?