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 c开发者_如何学运维an 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.
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.
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'.
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).
精彩评论