ZF-12438: "session has already been started by or session_start()" error message is misleading


On Ubuntu, garbage collection for sessions is taken care of by a cron job, rather than within PHP. If one downloads the PHP packages from Ubuntu's repository, then session.gc_probability in php.ini is set to 0, so PHP never runs the garbage collection for sessions. Unfortunately, if one changes session.gc_probability to something else, then one randomly encounters errors when the session garbage collection is run.

The error is something like this:

Fatal error: Uncaught exception 'Zend_Session_Exception' with message 'session has already been started by or session_start()' in /asps/www/library/Zend/Session.php:462
Stack trace: #0 /asps/www/library/Zend/Session/Namespace.php(143): Zend_Session::start(true)
             #1 /asps/www/library/Zend/Auth/Storage/Session.php(87): Zend_Session_Namespace->_construct('Zend_Auth')
             #2 /asps/www/library/Zend/Auth.php(91): Zend_Auth_Storage_Session->_construct()
             #3 /asps/www/library/Zend/Auth.php(141): Zend_Auth->getStorage()
             #4 /asps/www/application/layouts/scripts/layout.phtml(8): Zend_Auth->hasIdentity()

The issue appears to be that garbage collection is failing, and this causes Zend Framework to think that a session has already been started. The easiest way to test this that I've found is to set session.gc_probability to 1 and session.gc_divisor to 1, so that the error is triggered every time one tries to start a session. With the default values, the error usually only shows up once every hundred times the page loads. Of course, you'll need to be testing this on Ubuntu with one of their PHP packages to see this error.

It would be nice if errors could not be thrown in this case, or if the error message could be more descriptive. It took awhile to track down the cause of this error, but I eventually found the answer on Stack Overflow.


This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: