简体   繁体   English

关闭浏览器后清除缓存 - symfony2

[英]Clear cache after close the browser - symfony2

I'm using Symfony2 and I would like to logout the user after he closes the browser. 我正在使用Symfony2,我想在关闭浏览器后注销用户。

I've changed the cookie_lifetime to 0 inside of the config.yml, but still not working. 我已经将config.yml中的cookie_lifetime更改为0,但仍然无效。

The idea is: 这个想法是:

  • if the user doesn't use the REMEMBER ME, I would like to ask him to login in case of he close the browser and open again. 如果用户没有使用REMEMBER ME,我想请他登录,以便关闭浏览器并再次打开。

I found that there's no really reliable way check wether the browser-window/tab has been closed even if you use a cookie lifetime of 0 due to the way some browsers handle the cookies. 我发现,由于某些浏览器处理cookie的方式,即使你使用的cookie生存期为0,也没有真正可靠的方法检查浏览器窗口/标签是否已关闭。

However another - obviously not always reliable - way to check wether the window/tab was closed is using JavaScript's unload events. 然而另一个 - 显然并不总是可靠 - 检查窗口/标签是否关闭的方法是使用JavaScript的卸载事件。

The combination of both should give you the best results. 两者的结合应该会给你最好的结果。

Examples - vanilla JavaScript: 示例 - vanilla JavaScript:

window.onbeforeunload = function(){
  // send ajax request to invalidate the session
};

... or ... ... 要么 ...

window.unload = function(){
  // send ajax request to invalidate the session
};

Example - jQuery: 示例 - jQuery:

$(window).bind('beforeunload', function(){
  // send ajax request to invalidate the session
});

... or ... ... 要么 ...

$(window).unload(function(){
  // send ajax request to invalidate the session
});

More information can be found in this question . 可以在此问题中找到更多信息。

I assume you are using Chrome browser. 我假设您使用的是Chrome浏览器。 Chrome has a problem with clearing such cookies (see the discussion ) Chrome在清除此类Cookie时遇到问题(请参阅讨论

Other browsers (FF, Safari) works fine and deletes cookies when browser closes, try it youself. 其他浏览器(FF,Safari)工作正常,并在浏览器关闭时删除cookie,自己尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM