Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close/kill the session when the browser or tab is closed

Can somebody tell me how can I close/kill the session when the user closes the browser? I am using stateserver mode for my asp.net web app. The onbeforeunload method is not proper as it fires when user refreshes the page.

like image 653
Punit Avatar asked Dec 17 '09 13:12

Punit


People also ask

Does session expire on closing tab?

The user session should be terminated immediately if they close the browser tab or window.

How do you close a session in javascript?

You need to tell the server to kill a session variable. The only way to do that from javascript is to use Ajax to call some custom page, with for example as variable the session key you want to delete.


1 Answers

You can't. HTTP is a stateless protocol, so you can't tell when a user has closed their browser or they are simply sitting there with an open browser window doing nothing.

That's why sessions have a timeout - you can try and reduce the timeout in order to close inactive sessions faster, but this may cause legitimate users to have their session timeout early.

like image 103
Oded Avatar answered Oct 12 '22 01:10

Oded