SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php sessions - time out ?

    how do i time out a php sessions automatically after 3 minutes of inactivity ?

  2. #2
    SitePoint Enthusiast spoorw8er's Avatar
    Join Date
    Oct 2001
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First part of the solution:

    First when a new session for a user is started, store the time it started (in a table or in the session variables for that user).

    Then everytime the user activates (another) script by clicking on a link, in that script update the time recorded for the session to reflect the current one.
    And ofcourse you could check the time-difference before update the time and decide to kill the session if it is longer than 3 minutes since last activity (depends how friendly you want to be).

    This gives you the ability to keep track of last activity for a session.

    Second part of the solution:
    But how to detect inactivity? The user can close his browser, can wonder off to another site (by typing a new address or clicking a link that takes him out of your site) leaving you without a clue wether he's active or not (remember in part one we check the time when he does something).

    This means you will have to scan the last-activity times for all sessions on a regular basis, destroying (deleting the record, or marking a session as invalid, whatever) those that are idle for to long.

    You can either include this scan at the start of every script, which means the check will be done for all sessions everytime somebody (whoever he may be) activates a script. The disadvantage is that there is no way to close sessions after EXACTLY three minutes of inactivity (it might take ages before another visitor comes to your site).

    Or you can write a script that runs at a regular interval (using cron on Unix-systems for instance) that does the scan. This way you can garantuee a session will be 'destroyed' after exactly three minutes of inactivity.

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you can store your session data in a database, then cleaning out sessions is as simple as deleting from the sessions table any record whose time hasn't been updated in three minutes.


    Have a look here for some routines I wrote a while back to store session data in mysql table.

    http://www.sitepointforums.com/showt...+freddydoesphp
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •