Home » Php » php – When does session.gc_maxlifetime start counting

php – When does session.gc_maxlifetime start counting

Posted by: admin July 12, 2020 Leave a comment


On every page load I run session_start() to resume the current session. When does the session.gc_maxlifetime start counting from? The first time session_start() is called and that session’s cookie is set? Or does the timer restart every session_start()?

With the session.gc_maxlifetime set to about 24 minutes when in this timeline should the session data be garbage collected

 1. 12:00:00 First page load, session_start(), session cookie created
 2. 12:10:00 page load, session_start() 
 3. 12:26:00 page load, session_start()
 4. 12:55:00 page load, session_start()

If the timer starts on first page load session_start() then I would expect it would garbage collect on load 3. But if it resets every session_start(), then it wouldn’t collect until load 4.

I am hoping it it the latter because then I could do a javascript heartbeat every few minutes to call a page that runs session_start().

If it is the former, session.gc_maxlifetime starts counting from creation of session cookie I need to destroy/recreate or regenerate_id to reset the count?

How to&Answers:

It is from the last session_start() call OR/AND the last last time it is written to. My guess would be the latter since that would change the “last modified” timestamp that the PHP garbage collector would use to determine if it needs to be deleted or not. In which case, it starts counting from the last script to finish that uses session_start() or that explicitly finishes with it mid script by calling session_ write_ close().

P.S. This would make that “heartbeat” nice and simple (in one line): http://prototypejs.org/api/ajax/periodicalUpdater