PHP Sessions

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
Hi,

I have a simple logout script whereby the user the logout link and the following happens:

destroy session via session_destroy() and do a bit of database stuff like marking them as not logged in etc. This all works how I want it to --- as long as the user directly clicks the log out link.

Is there is any way to detect when a session/closes expires so I could run my above script?

Thanks
 
Only by reading back the session variable and see if it contains something. It would be useful to have a status flag in your session variable. Set it when you write to the session variable with some word or other.

If when you read it back its empty, then you can regard the session expired.

Session variables are server side and are only read when you request a page from the server.

Thats general and not specific to any language.
 
Hmmm :(

Guess I could bodge it a bit - E.g. include a bit of code in a daily cron job script to set the users status to offline based on the last login time (which is recorded at login). It's a bit hacky, but would work as I know users don't use the system outside working hours.
 
You could have a "last active" DATETIME for each of your users, update it on every pageload, and then set to "offline" those users whose "last active" timestamp is more than X minutes ago.

To set them offline, just run a simple delete statement at the top of your main include. This example will set users to offline who haven't been active in the last hour:

Code:
UPDATE users SET online = 0 WHERE DATE_ADD(last_active, 1 HOURS) < NOW()
 
You could have a "last active" DATETIME for each of your users, update it on every pageload, and then set to "offline" those users whose "last active" timestamp is more than X minutes ago.

To set them offline, just run a simple delete statement at the top of your main include. This example will set users to offline who haven't been active in the last hour:

Code:
UPDATE users SET online = 0 WHERE DATE_ADD(last_active, 1 HOURS) < NOW()

Great idea! Thanks
 
Back
Top Bottom