Jump to content

Recommended Posts

Maybe you can use your database. You can add a field named login_time to your users table to store the date and time of user's login time. Everytime users login, update that field with the current date and time. And, everytime users navigate to the other page, compare the current date and time with that login_time field content. If the gap between them is bigger than the defined time, destroy the session.

 

And since session_destroy() only deletes the session data file, which will get recreated with any existing $_SESSION variables when the script ends, it is actually pointless to use session_destroy().

 

To automatically log someone out after they have been inactive for a time, you store their last active time (usually in a database table) and then you check for any last active times older then a limit you set. You can either check on each new page request and switch them to logged out at the start of a page request where too much time has gone past from their last active time or you can use a cron job/scheduled task to periodically switch anyone to being logged out who's last active time is older than your limit.

 

Also, a session is just a container. Don't waste a lot of time or lines of code trying to change how sessions work or when they end. Your application should use specific data to determine if and when someone is logged in/logged out.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.