Thursday, September 6, 2012

PHP Session Has Already Started Error Explained

When you get the following exception, it could mean multiple things. It isn't just because output have started to be sent but rather some other weird stuff that relates to the exception.

PHP message: PHP Fatal error:  Uncaught exception 'Zend_Session_Exception' with message 'session has already been started by session.auto-start or session_start()' in ...

1. First, it probably is. You may have triggered the application to output something prior to starting the session. This means that you may have called a function or operation like "echo" before calling session_start().

2. Whitespaces or non-PHP (html or text) content prior to the PHP tag. Like the first, it triggers the web server to send output headers prior to even start parsing the script. Thus, triggering the said error. Be particularly watchful for libraries that you include. It may become very hard to debug if you do not know this fact.

3. Session handler is not able to write session data. This is particularly a problem for the default session handler which saves data to the disk. This means that if you lack the correct permission, then you will receive the same error as the above. If your error log messaging is enabled, you may find a more detailed error information which would indicate a 'permission denied' issue. If that is the case, then correcting the permission problem by providing write permission to folder to the PHP user of the server would solve it. Similarly, if session handlers like a database, memcache etc, goes down, you will most likely encounter the same problem.

Knowing these fundamental cases where problems may lie, should save you some time. If you're here, I hope I helped you made your life a little bit better.

  • Related Links Widget for Blogspot

No comments: