Jump to content
xisto Community
TavoxPeru

Run A Script When Expires A Session

Recommended Posts

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,

Share this post


Link to post
Share on other sites

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.

Edited by Jimmy89 (see edit history)

Share this post


Link to post
Share on other sites

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:

 

$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 :rolleyes:

Share this post


Link to post
Share on other sites

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:

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...

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. :rolleyes:vujsa

Share this post


Link to post
Share on other sites

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:

The php code will be very simple to develop and efficient.

Don't deal with cron jobs.

Don't overkill the server.

Best regards,

Share this post


Link to post
Share on other sites

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:

The php code will be very simple to develop and efficient.

Don't deal with cron jobs.

Don't overkill the server.

Best regards,
YAY, I WIN!!!!!!!!!!!!!!!!B)

 

Glad I could help. I actually answered this topic twice. :rolleyes:

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

Share this post


Link to post
Share on other sites

YAY, I WIN!!!!!!!!!!!!!!!!B)

 

Glad I could help. I actually answered this topic twice. :rolleyes:

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,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.