Jump to content


Photo

How To Destroy The Session When The User Closes The Tab Not A Whole Browser?


  • Please log in to reply
4 replies to this topic

#1 simbu

simbu

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 17 December 2012 - 04:57 AM

Hello,

I want to destroy the session when the user closes the particular in google chrome.So let me how to make or if u have sample code means post it.





regards,
sim.

#2 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 17 December 2012 - 06:53 AM

You'll have to attach an AJAX call to the onbeforeunload event. Though, be warned:
For Javascript there is no discernible difference between reloading, navigating away from the page, or closing it; Everything you do that causes the current page to be removed/redisplayed will trigger the event!
Keeping it simple.

#3 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,242 posts
  • LocationMississauga, Canada

Posted 27 December 2012 - 11:59 PM

Christian, why ?
It's not a server side issue, using an Ajax.
I don't get it your logic, can you explain me, please?

#4 DavidAM

DavidAM

    Advanced Member

  • Gurus
  • 1,949 posts
  • LocationSpring, TX USA

Posted 28 December 2012 - 12:12 AM

Sessions are stored on the server. More specifically, the data in a session is stored on the server. Generally, the client has only a cookie to identify what session it is using.

The server does not know anything about what happens on the client (the browser) until the browser sends a request. Generally, closing a tab or window or the entire browser application does NOT send any kind of request to the server. So, if you want the server to destroy the session on a page close, you have to send a message to the server. The only facility for doing that is to use AJAX in the beforeUnload event.

However, as Christian F. said, this event will be triggered when moving from one page to another on the same server; probably even on a hard refresh (F5 / CTRL-F5) of the same page. So, it is really not a good candidate for clearing the session.
-- I haven't lost my mind, it's backed up on tape ... somewhere!

#5 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,242 posts
  • LocationMississauga, Canada

Posted 28 December 2012 - 06:44 AM

OMG, sessions - I was talking about cookies :shrug:
I was so tired last night, sorry about that.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com