gpt4 book ai didi

JavaScript 没有获取所有 cookie

转载 作者:行者123 更新时间:2023-11-30 17:47:21 25 4
gpt4 key购买 nike

我正在尝试使用 JavaScript 获取存储的 cookie。以下是通过检查页面并转到资源在 Google Chrome 中显示的 cookie 列表。

enter image description here

当我运行代码时:

alert(document.cookie);

它只显示 K----S------C---- cookie,但不显示 adminhtml cookie。如何访问 adminhtml cookie?

============================================= ====

编辑:

根据选择的答案,JavaScript 无法访问仅限 HTTP 的 cookie。虽然我找到了解决方法。它可能不是最安全的,但在像这种需要获取 cookie 信息的情况下,试试这个。

使用PHP将cookie信息写入一个隐藏的div中:

<div id="adminhtml" style="visibility:hidden"><?php
echo $_COOKIE['adminhtml'];
?></div>

然后使用JavaScript获取div的innerhtml:

<script>
var cookieValue = document.getElementById("shopperid").innerHTML;
</script>

最佳答案

adminhtml 似乎是一个 HttpOnly cookie。您无法从 javascript 访问 HttpOnly cookie。

引用自维基百科,找不到官方文档:

The HttpOnly attribute is supported by most modern browsers. On a supported browser, an HttpOnly session cookie will be used only when transmitting HTTP (or HTTPS) requests, thus restricting access from other, non-HTTP APIs (such as JavaScript). This restriction mitigates but does not eliminate the threat of session cookie theft via cross-site scripting (XSS). This feature applies only to session-management cookies, and not other browser cookies.

关于JavaScript 没有获取所有 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19863935/

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