How to close the session if the user closes the 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
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

How to close the session if the user closes the window ?

Post by anjanesh »

How to close the session if the user closes the window and not chooses to logout ? I store the session is a table but they get removed only when logging out. But many times the users don't log out - they just close the window. In that case how do I get session_destroy() to work ?
Thanks
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

sessions automatically end when the browser is closed

to solve the problem for the mysql table

run a time function

basically if the time exceeds 2 hours from time logged in, run a query and update the database

this is how its done most of the time i believe
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

It is a pain in the ass.

Javascript is the way to go.

Unfortunately there isn't a window.onClose function in the Javascript specification and is only implemented by Some versions of Netscape.

So...

a solution i used (and probably the only solution) is to use the onUnload function

The problem with onUnload is that the functions is executed when the user navigates from page to page WITHIN the site...which is obviously no good.

To get around this...

Load the site using frames, one which is like 1 pixel in height and the other fills the rest of the screen. In the 1 pixel frame, load a html/php file that has onUnload in the body tag. When the user closes the broser window, the a pop-up window appears executing a "hard" log-out.

When the user is navigating the site as normal, the html/php file in the 1 pixel frame never changes, therefor not calling the onUnload event.

Mark
Post Reply