|
|
|
|
![]() ![]() |
Nov 25 2007, 02:06 AM
Post
#1
|
|
|
Super Member Group: [HOSTED] Posts: 746 Joined: 8-April 06 From: Lima - Peru Member No.: 12,579 |
For example, when a user logins to a page -with a login form- and after validating and verifying it's credentials i store some information related to this user in session variables and a table with his state -connected- is updated, then i use it in other pages, etc.
When this user logouts -by clicking a logout link- i release -unregister, destroy, etc- all the session variables stored and the same table is updated with his state -disconnected-. All of this funcionality works very well, the problem comes when the user do not click on the logout link and the session expires, the user state is not updated because of this and remains connected. How can i run a php script when a session expires??? Is it possible to do this??? Best regards, |
|
|
|
Nov 25 2007, 09:43 AM
Post
#2
|
|
|
Living at the Datacenter Group: [HOSTED] Posts: 696 Joined: 30-June 06 From: Australia Member No.: 14,219 |
you could possibly run a cron job every 15-20 minutes (i know that puts strain on the gamma server) that checks if there has been any activity by logged in users, if not, run their expiry script! you may want to run it more frequently to keep the information more up to date, but that could be a huge strain on the web server, if you run it for users that are 'supposed' to be logged in, that would be better then running it for all users.
This post has been edited by Jimmy89: Nov 25 2007, 09:43 AM |
|
|
|
Nov 25 2007, 11:19 AM
Post
#3
|
|
|
Nenad Bozidarevic Group: [MODERATOR] Posts: 1,002 Joined: 7-November 05 From: Belgrade, Serbia Member No.: 9,500 |
A Cron Job would be the easiest, but not the most efficient solution, as it may be an overkill for the server.
First of all, you would have to have a MySQL column indicating the time of the last activity. If you run the script every 15 minutes, let's say you want to log out users that haven't done anything during those 15 minutes. Supposing that you store that last activity time as a UNIX timestamp in the database, this would be the simplest query: CODE $query = 'SELECT `what_you_need` FROM `table` WHERE `logged_in` = 1 AND `last_act` + 900 < '.time(); After that, all you have to do is log out each result row. You may also want to have a look at how different bulletin boards function. IP.Board, for example, doesn't use Cron Jobs at all, but has "user(s) active in the past 15 minutes". Since there are many big forums using this software, the script it uses obviously isn't too greedy |
|
|
|
Nov 25 2007, 05:54 PM
Post
#4
|
|
|
Absolute Newbie Group: Admin Posts: 887 Joined: 20-February 05 From: Indianapolis, Indiana, USA (Midwest) Member No.: 2,714 |
I'm assuming that you store your session data in a database.
Instead of using a CRON job, I suggest placing code in you main script to run every time a page is requested. This way the next user activates the script that cleans up your session table. Like so: SQL UPDATE table_session SET active = 0 WHERE session_time < UNIX_TIMESTAMP() - 20*60 AND active = 1 Update all active records in table_session to inactive where session_time is less than the the current time minus 20 minutes. This will add one additional SQL query to each page load but will provide you with an auto-logout system for users that don't log out. You could also reset the last session time to when the user should have been logged out... SQL UPDATE table_session SET active = 0, session_time = session_time + 20 * 60 WHERE session_time < UNIX_TIMESTAMP() - 20*60 AND active = 1 This way you would be able to see when the user was logged out if you care. Be sure you limit the search query to active sessions only to reduce server load and place an index on that column would assist the server further. By using the UPDATE query, the script does two jobs at once, it looks for expired sessions and updates the information as needed. There is no need to run a SELECT query in this case since you should need to return any data at this time and the items you would select are the ones that need to be updated anyway. UPDATE do its thing and if there are no records to update then it will just return zero rows affected... Hope this helps. vujsa |
|
|
|
Nov 26 2007, 07:26 AM
Post
#5
|
|
|
Super Member Group: [HOSTED] Posts: 746 Joined: 8-April 06 From: Lima - Peru Member No.: 12,579 |
First of all, thanks everybody for your really helpful information.
And second, based on this information i decide to follow the suggestions made by vujsa to resolve this issue because in my opinion:
|
|
|
|
Nov 26 2007, 08:32 PM
Post
#6
|
|
|
Absolute Newbie Group: Admin Posts: 887 Joined: 20-February 05 From: Indianapolis, Indiana, USA (Midwest) Member No.: 2,714 |
First of all, thanks everybody for your really helpful information. And second, based on this information i decide to follow the suggestions made by vujsa to resolve this issue because in my opinion:
YAY, I WIN!!!!!!!!!!!!!!!! Glad I could help. I actually answered this topic twice. I wrote a nice reply then decided not to submit it because I wasn't sure if you were storing your session data in a database. If you were storing all of the information in the server's session path, then the process is much different and I'm not sure how I would do it much less try and explain to you how to do it. Then after reading the other replies, I decided to go ahead and reply. I'm a huge fan of having what ever user is online currently trigger global system events like this. Of course, if you site isn't very busy then it won't work very well but if it isn't busy, then there would be anyone there to notice that everyone is still logged in. Good Luck, vujsa |
|
|
|
Dec 5 2007, 02:19 AM
Post
#7
|
|
|
Super Member Group: [HOSTED] Posts: 746 Joined: 8-April 06 From: Lima - Peru Member No.: 12,579 |
YAY, I WIN!!!!!!!!!!!!!!!! Glad I could help. I actually answered this topic twice. I wrote a nice reply then decided not to submit it because I wasn't sure if you were storing your session data in a database. If you were storing all of the information in the server's session path, then the process is much different and I'm not sure how I would do it much less try and explain to you how to do it. Then after reading the other replies, I decided to go ahead and reply. I'm a huge fan of having what ever user is online currently trigger global system events like this. Of course, if you site isn't very busy then it won't work very well but if it isn't busy, then there would be anyone there to notice that everyone is still logged in. Good Luck, vujsa Well i try both ways, storing in a database -which is the one i currently use- and storing all the information in the server's session path, could you explain how to do it with the second one???? maybe it will be more efficient, do you have test both of them??? Best regards, |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 5th September 2008 - 01:10 PM |