Reputation: 477
I would like to know whether there is any method such that when a user close a browser, it will kill a web server session?
I know some bank website can perform this task, but dont know how to implement it.
Thanks.
Upvotes: 0
Views: 4846
Reputation: 3755
In short from client side you can't do this.
As web applications use http, and http is stateless, you can not know for sure what happens on client side, since there is no connection between client and server. The only thing that can be used to keep track of it is the session. And the session is kept by the server as long as it was set up to be kept, or the application is closing it. So if you want to close the session, you have to initiate it from the server. Thus the server side part of your application has to be notified about browser closure. Deleting the session cookie on client side won't close the session on server side.
Upvotes: 0
Reputation: 27843
You can use a heartbeat algorithm to achieve it. Basically the web page "pings" (using AJAX) the server every once in a while (1 min for example), keeping the session alive. Configure the server to have a low session timeout time (3 min for example) and voila.
Obviously this does not terminate the session exactly when the user closes the browser, but it's a very good substitute for most use cases.
Upvotes: 1
Reputation: 600
You can use Ajax and jquery to implement it, Call browser close event using jquery and make ajax call and kill that particular session object
Upvotes: 0