*真的*用 javascript 删除 cookie
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是不可能的。时期。我已经为此苦苦挣扎了几个星期,但没有找到解决方案。
发明
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'.
除了设置过去的过期时间外,还可以将该值设置为空字符串。这至少会立即减小 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.
您可以用新的 cookie 覆盖该 cookie。
You could overwrite the cookie with a new one.
如果您无法控制通过设置cookie的javascript(这看起来非常奇怪,为什么你没有控制权?),你可以不断地读取和清空cookie,将数据转储到另一个更大的数据库(最好是服务器端,或者可能是HTML5客户端存储) )。
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).