Dedstroying session , on closing the browser window

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
napster
Forum Newbie
Posts: 1
Joined: Wed Jul 14, 2010 2:05 am

Dedstroying session , on closing the browser window

Post by napster »

hey guyz , i m in a big hell while creating a chat server , me getting a problem that when ever a user logout , the session successfully destroys , but on closing the browser window without logging out , marks the user as available in other's account ...
Can anyone help me how to distroy a session , by closing the browser window , without clicking on log out ....????


User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Dedstroying session , on closing the browser window

Post by Apollo »

You can't force a browser to send a logout notification before it closes (or even when it closes the page).

What you could do it is keep sending "I'm still online" notifications in the background using javascript / Ajax. If you are not receiving any updates anymore, you know the user is offline (or his browser crashed, or his internet was disconnected, or he's moving to a different WiFi spot, or he put his laptop to sleep/hibernate, or whatever - probably all valid reasons to consider him 'gone').
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: Dedstroying session , on closing the browser window

Post by buckit »

you could try using the onunload event. so when the browser is closed it opens a popup window that runs your logout script and closes the popup. in theory that should work. have never tried it.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Dedstroying session , on closing the browser window

Post by Apollo »

In theory (and perhaps some practical sitations) it will work. But there are plenty of scenarios where it certainly won't.
Post Reply