使用 Javascript 删除 cookie

发布于 2025-01-06 20:21:42 字数 513 浏览 3 评论 0原文

如果我使用代码设置 cookie

var a = 'jn=900; expires=Fri, 27 Jul 2013 02:47:11 UTC; path=/';

document.cookie = a;

那么可以肯定 document.cookie = 'jn=900;过期= UTC 时间 1999 年 7 月 27 日星期五 02:47:11; path=/'; 将删除 cookie。

删除cookie是否需要设置所有用于设置cookie的属性?

IE。是否 document.cookie = 'jn=900; expires=Fri, 27 Jul 1999 02:47:11 UTC;' 足以删除上面显示的 cookie,或者我还应该指定用于设置 cookie 的路径吗?

假设在我要删除 cookie 的页面中可以访问 cookie。

是否可以使用 Javascript 删除 PHP 设置的 cookie?

If i set a cookie using the code

var a = 'jn=900; expires=Fri, 27 Jul 2013 02:47:11 UTC; path=/';

document.cookie = a;

Then it is sure that document.cookie = 'jn=900; expires=Fri, 27 Jul 1999 02:47:11 UTC; path=/'; will delete the cookie.

Is it necessary to set all the properties used to set the cookie for deleting the cookie?

Ie. whether document.cookie = 'jn=900; expires=Fri, 27 Jul 1999 02:47:11 UTC;' is enough for deleting the cookie shown above, or should I also specify the path as used to set the cookie?

Assume that the cookie is accessible in the page I'm deleting the cookie.

Also is it possible to delete a cookie set by PHP using Javascript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2025-01-13 20:21:42

设置路径是一种很好的做法,可以避免在不同域(www.domain.com 而不是 domain.com)上错误设置 cookie 等问题。

关于第二个问题,答案是肯定的,如果PHP创建的cookie没有标记为HttpOnly,您可以使用Javascript访问和删除它们。

HttpOnly 属性指示浏览器仅通过 HTTP 协议使用 cookie。 HttpOnly cookie 无法通过非 HTTP 方法访问,例如通过 JavaScript 调用(例如,引用“document.cookie”),因此不能通过跨站点脚本轻松窃取(一种普遍的攻击技术[27])。如前面的示例所示,Facebook 和 Google 都广泛使用 HttpOnly 属性。

http:// /en.wikipedia.org/wiki/HTTP_cookie#HttpOnly_cookie

It is a good practice to set the path to avoid issues like cookies set by mistake on a different domain (www.domain.com instead of domain.com).

Regarding the second question, the answer is yes, you can use Javascript to access and delete cookies created by PHP if they are not marked as HttpOnly.

The HttpOnly attribute directs browsers to use cookies via the HTTP protocol only. An HttpOnly cookie is not accessible via non-HTTP methods, such as calls via JavaScript (e.g., referencing "document.cookie"), and therefore cannot be stolen easily via cross-site scripting (a pervasive attack technique[27]). As shown in previous examples, both Facebook and Google use the HttpOnly attribute extensively.

http://en.wikipedia.org/wiki/HTTP_cookie#HttpOnly_cookie

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文