如何让我的网站在用户关闭浏览器时自动注销?
假设我有一个登录表单。当我成功登录后,我将被重定向到已登录的主页。目前,当我关闭此页面而不注销并重新打开 Firefox 时,此登录主页将再次启动。
我希望这样当用户关闭浏览器时,会话就会过期,并且当他们下次打开 Firefox 时,不会显示登录的主页。
我认为会话将用于此目的,但我不知道如何设置时间,甚至不知道如何设置时间,以便在 Firefox 关闭时关闭会话被销毁。
Let's suppose I have a sign-in form. When I sign in successfully I am redirected to the logged in home page. Currently, when I close this page without signing out, and re-open Firefox, this logged in home page is started again.
I want it so that when the user closes their browser the session is the session is expired and when they next open Firefox the logged-in home page isn't displayed.
I think session will be used for this, but I don't know how I can set a time or even make it so that when Firefox closes the closes session get destroyed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您希望会话 cookie 的过期时间为 0 - 请参阅:
http://php .net/manual/en/function.setcookie.php
http://docs.php.net/manual/en/session.configuration.php#ini.session.cookie-lifetime
You want the session cookie to have an expire time of 0 - see:
http://php.net/manual/en/function.setcookie.php
http://docs.php.net/manual/en/session.configuration.php#ini.session.cookie-lifetime
将会话超时设置为 15 分钟,这应该可以完成工作。您无法确定捕获“关闭浏览器”或“关闭选项卡”事件(未启用 JavaScript,浏览器只是崩溃等),因此您不应该这样做。
检查 Felix 的帖子以获取在何处准确设置会话超时的其他信息。
Set your session timeout to 15 minutes, this should do the job. You cannot catch the "close browser" or "close tab" event for sure (no javascript enabled, browser just crashes, etc), so you shouldn't go for that.
Check Felix' post for additional information where to set the session timeout exactly.
如果我作为非服务器 HTML 程序员感到不舒服,请原谅我,但是您能检查一下浏览器是否已经有来自上一个会话的 cookie 吗?即使答案是肯定的,如果是新会话,是否强制进行身份验证?
只是一个想法。
Pardon me if I am stepping on toes as non-server HTML programmer, but could you check to see if the browser already has a cookie from a previous session? Even if the answer is yes, if it is a new session, force an authentication?
Just a thought.