且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用户离开页面时使Laravel中的登录无效

更新时间:2023-12-01 20:43:46

简短答案:您不能

整个浏览器关闭时,可以通过简单地在 config/session.php 中设置 expire_on_close 来破坏会话(也请确保您清除浏览器中的cookie使其起作用):

The session can be destroyed when the entire browser is closed by simply setting expire_on_close in config/session.php (also make sure you clear the cookies in your browser for this to work):

'expire_on_close' => true,

但是无法检测到何时仅在浏览器中关闭了标签页.您将拥有的最接近的东西是JavasSript方法 onbeforeunload 在您关闭标签页时触发.但是,当您离开页面导航或单击后退"按钮时,也会触发此操作,并且无法区分这些操作.

But there is no way to detect when only a tab is closed in the browser. The closest thing you would have is the JavasSript method onbeforeunload that triggers when you close a tab. But it also triggers when you navigate away from a page or hit the back button, and there's no way to differentiate between those actions.

您可以在服务器上设置很短的会话时间,然后在任何打开的页面上" ping "服务器,以使其保持会话状态,这意味着它将关闭打开应用程序的选项卡时,它很快就会过期,但这是一个非常丑陋的解决方案.

You could set a very short session time on the server, and "ping" the server from any open page, to let it know to keep the session still in use, which would mean it would very quickly expire when the tabs that have the app open are closed, but that's an extremely ugly solution.