Session Destroy When One Browser Tab Gets Closed

Session Destroy when one browser tab gets closed

Due to the nature of the client/server model there is no easy way to do what you want. If the user may stay idle for a while after loading your page, he can also close the tab and open it again. You have no control of what the clients do in the client side. There are simple javascript methods to do this such as the unload event, but they are not reliable and often don't work.

You may use websockets to ensure the client is always connected or do several ajax requests in the background and keep a timeout of a few seconds in your session, but those methods will disconnect the user if his internet connection drops even for a few moments.

How to clear session on closing the browser tab?

Browsers only destroy session cookies when the entire browser process is exited. There is no reliable method to determine if/when a user has closed a tab. There is an onbeforeunload handler you can attach to, and hopefully manage to make an ajax call to the server to say the tab's closing, but it's not reliable.

And what if the user has two or more tables open on your site? If they close one tab, the other one would effectively be logged out, even though the user fully intended to keep on using your site.

by : Marc b

After session destroy or close browser tab or close browser execute logout using Laravel 5.2

The server does not know if the user has closed the browser window. You need to detect this event via javascript on the client side and notify the server manually.

See this answer: javascript detect browser close tab/close browser



Related Topics



Leave a reply



Submit