We need to keep a session alive for 30 minutes and then destroy it?
We need to implement our session timeout. The options are(session.gc_maxlifetime and
session.cookie_lifetime)(http://php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime) are not reliable
.First Option:
session.gc_maxlifetime
- session.gc_maxlifetime specifies the number of seconds after which data will be seen as ‘garbage’ and cleaned up. Garbage collection occurs during session start.
Second Option:
session.cookie_lifetime
- session.cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser.
Best solution:
- Use a simple time stamp that denotes the time of the last activity (i.e. request) and update it with every request:
- Updating the session data with every request also changes the session file’s modification date hence, the previous sessions are not removed from the data.
- Use an additional time stamp to regenerate the session ID periodically to avoid attacks on sessions like session fixation:
We can particle sessions after a certain lifespan by using the session.gc_maxlifetime( http://uk3.php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime)ini setting:
[pastacode lang=”php” manual=”%24_SESSION%5B’example’%5D%20%3D%20array(‘foo’%20%3D%3E%20’bar’%2C%20’registered’%20%3D%3E%20time())%3B%0A%0A%20%2F%2F%20later%20%0A%0Aif%20((time()%20-%20%24_SESSION%5B’example’%5D%5B’registered’%5D)%20%3E%20(60%20*%2030))%0A%20%7B%20%0Aunset(%24_SESSION%5B’example’%5D)%3B%20%0A%7D%20%0A” message=”php code” highlight=”” provider=”manual”/]- Is .htaccess file to set the expire time ? Check with the below code:
- Here is the another Sample code:
- Use the session_set_cookie_params function .
- It automatically calls the function before session_start() call.
- Simply use the below sample code in our include file which loaded in every pages.
- Store a timestamp in the session
- Now, Check if the timestamp is within the allowed time window (1800 seconds is 30 minutes)