Javascript Chrome 不会删除会话 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10617954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:10:54  来源:igfitidea点击:

Chrome doesn't delete session cookies

javascriptsessiongoogle-chromecookies

提问by mgs

I'm trying to set session cookie in javascript like this:

我正在尝试在 javascript 中设置会话 cookie,如下所示:

document.cookie = 'name=alex; path=/'

But Chrome doesn't delete it even if I quit browser and launch it again.

但是即使我退出浏览器并再次启动它,Chrome 也不会删除它。

I checked in Firefox and Opera and both work as intended - they delete session cookie on browser exit.

我检查了 Firefox 和 Opera,它们都按预期工作 - 它们在浏览器退出时删除会话 cookie。

Is Chrome just ignoring expiration rules?

Chrome 是否只是忽略了过期规则?

I checked in multiple OSes and found out that session cookie gets removed on Chrome in Windows XP and Ubuntu, but NOT in Mac OSX Lion.

我检查了多个操作系统,发现会话 cookie 在 Windows XP 和 Ubuntu 中的 Chrome 上被删除,但在 Mac OSX Lion 中没有。

回答by Nikola Kolev

I just had the same problem with a cookie which was set to expire on "Browsing session end".

我刚刚遇到了与设置为在“浏览会话结束”时过期的 cookie 相同的问题。

Unfortunately it did not so I played a bit with the settings of the browser.

不幸的是它没有,所以我玩了一下浏览器的设置。

Turned out that the feature that remembers the opened tabs when the browser is closed was the root of the problem. (The feature is named "On startup" - "Continue where I left off". At least on the current version of Chrome).

原来,在浏览器关闭时记住打开的选项卡的功能是问题的根源。(该功能被命名为“启动时”-“从我离开的地方继续”。至少在当前版本的 Chrome 上)。

This also happens with Opera and Firefox.

这也发生在 Opera 和 Firefox 上。

回答by NSjonas

I just had this issue. I noticed that even after I closed my browser I had many Chrome processes running. Turns out these were each from my Chrome extension.

我刚刚有这个问题。我注意到即使在我关闭浏览器后,我也有许多 Chrome 进程在运行。原来这些都来自我的 Chrome 扩展程序。

Under advanced settings I unchecked 'Continue running background apps when Google Chrome is closed'and my session cookies started working as they should.

在高级设置下,我取消选中'Continue running background apps when Google Chrome is closed',我的会话 cookie 开始正常工作。

Still a pain in the rear for all of us developers that have been coding expecting that session cookies would get cleared when the user is done browsing.

对于我们所有的开发人员来说仍然是一个痛苦,他们一直在编码,希望在用户完成浏览后会话 cookie 会被清除。

回答by Justmyhobby

This maybe because Chrome is still running in background after you close the browser. Try to disable this feature by doing following:

这可能是因为关闭浏览器后 Chrome 仍在后台运行。尝试通过执行以下操作禁用此功能:

  1. Open chrome://settings/
  2. Click "Show advanced settings ..."
  3. Navigate down to System section and disable "Continue running background apps when Google Chrome is closed". This will force Chrome to close completely and then it will delete session cookies.
  1. 打开 chrome://settings/
  2. 单击“显示高级设置...”
  3. 向下导航到“系统”部分并禁用“Google Chrome 关闭时继续运行后台应用程序”。这将强制 Chrome 完全关闭,然后删除会话 cookie。

However, I think Chrome should check and delete previous session cookies at it starting instead of closing.

但是,我认为 Chrome 应该在开始而不是关闭时检查并删除以前的会话 cookie。

回答by emottet

I had to both, unchecked, under advanced settings of Chrome :

我不得不在 Chrome 的高级设置下取消选中两者:

  • 'Continue running background apps when Google Chrome is closed'
  • "Continue where I left off", "On startup"
  • '谷歌浏览器关闭时继续运行后台应用程序'
  • “从上次中断的地方继续”、“启动时”

回答by TimDog

A simple alternative is to use the new sessionStorageobject. Per the comments, if you have 'continue where I left off' checked, sessionStorage will persist between restarts.

一个简单的替代方法是使用新的sessionStorage对象。根据评论,如果您选中了“从我离开的地方继续”,则 sessionStorage 将在重新启动之间持续存在。

回答by TurKux

I had the same problem with "document.cookie" in Windows 8.1, the only way that Chrome deletes the cookie was shutting it from task manager (not a really fancy way), so I decided to manage the cookies from the backend or use something like "js-cookie".

我在 Windows 8.1 中遇到了与“document.cookie”相同的问题,Chrome 删除 cookie 的唯一方法是从任务管理器中关闭它(这不是一个非常奇特的方式),所以我决定从后端管理 cookie 或使用一些东西像“js-cookie”。

回答by Salman Ali Khan

Have you tried to Remove hangouts extension in Google Chrome? because it forces chrome to keep running even you close all the windows.

您是否尝试过在 Google Chrome 中删除环聊扩展程序?因为即使您关闭所有窗口,它也会强制 chrome 继续运行。

I was also facing the problem but it resolved now.

我也遇到了这个问题,但现在解决了。

回答by Mihai Perju

Go to chrome://settings/content/cookies?search=cookies

chrome://settings/content/cookies?search=cookies

Enable Clear cookies and site data when you quit Chrome.

启用Clear cookies and site data when you quit Chrome

Worked for me

为我工作

回答by SurferJoe

If you set the domain for the php session cookie, browsers seem to hold on to it for 30 seconds or so. It doesn't seem to matter if you close the tab or browser window.

如果您为 php 会话 cookie 设置域,浏览器似乎会保留它 30 秒左右。关闭选项卡或浏览器窗口似乎无关紧要。

So if you are managing sessions using something like the following it may be causing the cookie to hang in the browser for longer than expected.

因此,如果您使用类似以下内容管理会话,则可能会导致 cookie 在浏览器中的挂起时间比预期的要长。

ini_set("session.cookie_domain", 'www.domain.com');

The only way I've found to get rid of the hanging cookie is to remove the line of code that sets the session cookie's domain. Also watch out for session_set_cookie_params() function. Dot prefixing the domain seems to have no bearing on the issue either.

我发现摆脱挂起 cookie 的唯一方法是删除设置会话 cookie 域的代码行。还要注意 session_set_cookie_params() 函数。域前缀点似乎也与该问题无关。

This might be a php bug as php sends a session cookie (i.e. PHPSESSID=b855ed53d007a42a1d0d798d958e42c9) in the header after the session has been destroyed. Or it might be a server propagation issue but I don't thinks so since my test were on a private servers.

这可能是一个 php 错误,因为 php 会PHPSESSID=b855ed53d007a42a1d0d798d958e42c9在会话被销毁后在标头中发送一个会话 cookie(即)。或者它可能是服务器传播问题,但我不这么认为,因为我的测试是在私人服务器上进行的。