views:

26

answers:

3

I am using PHP for a project. I have set the session life to 0 so that when the user closes the browser, the session dies and he/she is logged out. However, I have a status variable in the DB which stores the information telling me if the user is logged in or not. I use this to inform other users the status of a particular user.

When the session dies, how can I call a function that will change the value in my DB? I have looked at overriding the session_set_save_handler(). But that requires me to override the entire function and define my own sessions. Is there a function that is called that I could use to change my DB variable?

Is there a better way for me to implement what I am trying to achieve?

Thanks

Edit: For those in the same situation as I am, I did what nikic suggested. I have a log which keeps track of each page visited by every user. In order to check if a user is online I check my DB to check if the logged in variable is set and then double check if there has been some activity in the recent past to know if the user is actually online.

+1  A: 

No easy way of doing so. Normally sites save the last action of an user in the database and say that he's only when he has done an action in the last N minutes. Additionally you can set the last action time to 0 if the user manually logs out, to take that into account, too.

nikic
I have thought of that idea, but that would mean I have to set up a cron job to find that last action time of a user. I am trying to find a better solution if possible. It seems like I will end up overriding the session_set_save_handler()
Nandit Tiku
@Nandit: I don't really get why you need a cron job for this. You simply save the timestamp in the DB and when you want to list all the users only you do a `SELECT WHERE lastAction > DATE_SUB(NOW(), INTERVAL 5 MINUTES)` or something similar.
nikic
Of course. I dont know what I was thinking. Thanks. That saves me a lot of trouble.Edit: But this would mean I have to update the DB every time the user requests a page. I wanted to avoid those extra DB calls if possible
Nandit Tiku
Thanks a lot. I did what you suggested and it works great. Too bad there isn't an easier/cleaner way of implementing this in PHP.
Nandit Tiku
A: 

Actually sessions with expire time = 0 will die every time a user does a request. The server has no way of knowing if the browser is open or closed, and this is why a session expires after X minutes (and why we have the whole session concept in the first place).

cambraca
I set the lifetime of sessions to 0 using session_set_cookie_params().The value 0 means "until the browser is closed."Source: http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-lifetime
Nandit Tiku
well session_set_cookie_params is about the cookie (client-side), not the session (server-side)
cambraca
A: 

PHP's session garbage collector doesn't provide any callback mechanism that could trigger a script to update your 'user is online' variable. It would be very nice, but it's not in PHP these days.

Assuming you're on the default file-based session handler, you could trivially whip up a script that scans the session save directory for sessions files older than your timeout period and update your database based on that. "If session file is older than 30 minutes, set user to 'logged out' in database". To do this you could either store the user's database ID in the session file, and extract that (session files are just the serialize()'d contents of $_SESSION[]), or extract the session ID from the file name ('sess_' . session_id() by default) and use that to update the database.

Can't see how this would be any more than 20 or 30 lines of code, unless you've got a huge DB interface library to handle. With the SPL DirectoryIterator, it's trivial to scan files for their 'mtime'.

Marc B