How do I detect if a session has ended?

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
Black Unicorn
Forum Commoner
Posts: 48
Joined: Mon Jun 16, 2003 9:19 am
Location: United Kingdom

How do I detect if a session has ended?

Post by Black Unicorn »

I am trying to build a simple chatroom, and it's going really well.
However, when it comes to updating the users-online list, I can't figure out how to get the script to figure out if a person has abandoned his/her session :(

Sofar I've set it up to add a user to a list upon login, i.e. $sql="insert into users_online '{$_SESSION["user"]}' where id=$blah" which works like a charm and a similar removal process when a person logs out. But what if a person doesn't log out, closes his browser or loses the connection?

With thanks for any attention.
Regards,
H
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I think I'd use some sort of timer. If you've been inactive in say 10 minutes, you should be deleted. So store the a 'last_active' timestamp in the database also.

If the 'last_active' < 10 minutes, do not show them in the list.
If he/she appears again on the 11th minute, require a new login, or use the old one if it's still applied.

Using the old session stored, might get you a big database, so you could consider cleaning it where 'last_active' > 3days old.

Just ideas. =)
Post Reply