*真的*用 javascript 删除 cookie

发布于 2024-11-27 09:17:51 字数 476 浏览 1 评论 0原文

在javascript中删除cookie的方法是将过期日期设置为过去的日期。现在这实际上并没有删除 cookie,至少在 Firefox 中是这样。这只是意味着 cookie 将在浏览器关闭时被删除。

这对我们来说是一个问题:我们有一个产品,涉及归档来自潜在许多站点的网页,所有这些内容都存储在我们的服务器上。为了确保页面正确渲染,我们还包含所有 js。然而cookie通常是由js设置的,并且考虑到页面缓存在我们的服务器上,这些cookie是在我们的域下设置的。

因此,随着时间的推移,来自数十个存档站点的 cookie 会在我们的域下建立起来。最终 Cookie 标头超出最大内容长度,导致 HTTP 400 错误代码。

而且由于我们的客户大多在企业环境中,他们永远不会重新启动计算机或关闭浏览器:它们可能会持续运行数月。所以这种“软”删除不起作用,至少不可靠。

有没有办法在 javscript 中物理删除会话内的 cookie?或者,有什么办法可以阻止它们被设置吗?

The way to delete cookies in javascript is to set the expiry date to be in the past. Now this doesn't actually delete the cookie, at least in Firefox. It just means the cookie will be deleted on browser close.

This is a problem for us: we have a product that involves archiving web pages from potentially many sites, with all this content stored on our server. And to make sure that pages render properly we include all js as well. However often cookies are set by js, and given that the page is cached on our server, these cookies are set under our domain.

So over time cookies from dozens of archived sites build up under our domain. And eventually the Cookie header exceeds the max content length, resulting in an HTTP 400 error code.

And because our clients are mostly in corporate environments they never reboot their machines or close their browsers: they can be left on for months. So this "soft" delete doesn't work, at least not reliably.

Is there any way to physically remove cookies intra-session in javscript? Or alternatively, is there any way to stop them being set?

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

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

发布评论

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

评论(4

不即不离 2024-12-04 09:17:51

这是不可能的。时期。我已经为此苦苦挣扎了几个星期,但没有找到解决方案。

发明 cookie getter/setter 的人应该是 %insert_painful_punishment_here%。

尤其是 Internet Exploder 在删除 cookie 方面堪称猛兽。我不记得确切的问题,但我认为它涉及 https 和包含 ; 的 cookie 名称。

我所能提供的只是一个解决方法:发送带有 400 响应的响应正文,例如“请重新启动浏览器”。

It's not possible. Period. I've been struggling with this for several weeks without finding a solution.

Whoever invented the cookie getter/setter should be %insert_painful_punishment_here%.

Particularly Internet Exploder is a beast when it comes to deleting cookies. I can't remember the exact issue, but I think it involved https and cookie names containing ;.

All I can offer is a workaround: Send a response body with your 400 response, something like 'please restart your browser'.

决绝 2024-12-04 09:17:51

除了设置过去的过期时间外,还可以将该值设置为空字符串。这至少会立即减小 cookie 的大小。

我认为所有浏览器中的 cookie 都应该立即删除。例如,当我退出网站时,Firefox 不需要我关闭浏览器来删除显示我已登录该网站的 cookie。如果这种情况没有发生,我建议您研究一下 Firefox 错误,并可能使用它们打开一个新错误。

与此同时,我会查看我的网络服务器,看看是否可以将最大内容长度设置为比现有长度更高的值。

In addition to setting the expiration in the past, set the value to an empty string. This will at least reduce the size of the cookie immediately.

I would think that cookies should be deleted immediately in all browsers. For example, when I log out of a website, Firefox does not require me to close my browser to delete the cookie that shows that I am logged into the site. If this isn't happening, I suggest you look into Firefox bugs and possibly open a new one with them.

In the meantime, I'd look at my web server and see if it is possibly to set the max content length to something higher than it already is.

夜访吸血鬼 2024-12-04 09:17:51

您可以用新的 cookie 覆盖该 cookie。

You could overwrite the cookie with a new one.

遗失的美好 2024-12-04 09:17:51

“这是因为我们没有使用 iframe,所以出现了此问题。缓存的页面由我们的服务器呈现,因此所有 cookie 都会在我们的域下设置。” --OP

如果您无法控制通过设置cookie的javascript(这看起来非常奇怪,为什么你没有控制权?),你可以不断地读取和清空cookie,将数据转储到另一个更大的数据库(最好是服务器端,或者可能是HTML5客户端存储) )。

"It is because we are NOT using iframes that we have this issue. The cached page is being rendered by our server, so any cookies get set under our domain." --OP

If you have no control over the javascript that is setting the cookies (which seems extremely odd, why do you not have control?), you can constantly read and empty the cookie, dumping the data to another larger database (preferably server-side, or perhaps HTML5 client storage).

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