Invalidate login in Laravel when user leaves the page

蹲街弑〆低调 提交于 2021-02-16 17:57:50

问题


It seems a small and easy to solve problem, but I cant find any solution for it.

I want to login the users only for that session while the are browsing the site. As soon as they close the tab I want to make their session expired. As much as I know I can't do this in the built-in Auth class.

How could I do this efficiently?


回答1:


Short answer: YOU CAN'T

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,

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.


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.




回答2:


Since PHP is server side code, it only knows the last time/page that was accessed.

See: Destroy PHP session on page leaving



来源:https://stackoverflow.com/questions/33760607/invalidate-login-in-laravel-when-user-leaves-the-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!