Zend_Session: Session must be started before any output has been sent to the browser

梦想的初衷 提交于 2019-12-01 22:11:40

问题


I've run into this issue before, but I can't remember how to solve it. I have created a bare bones (can't get any simpler) controller, and am just trying to echo something to the browser, and I am getting this message:

Fatal error: Uncaught exception 'Zend_Session_Exception' with message 'Session must be started before any output has been sent to the browser ...

Here's my entire controller. It's displaying 'success', but it also displays the error message. How can I silence that error message so I can simply echo something to the browser?

<?php

class CacheController extends Zend_Controller_Action
{
    public function clearAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        try {
            $result = Model_Cache::emptyCache(array('foobar'=>1));

            if ($result['status'] == true) {
                echo 'Success';
            } else {
                echo 'Error: ' . $result['message'];
            }
        } catch (Exception $e) {
            echo 'Error: ' . $e->getMessage();
        }
    }
}

回答1:


From memory, this error is usually due to non-PHP code starting the output before intended (and session initialisation). Usually it's due to whitespace or carriage returns after unnecessary ?> tags. This is the first thing I'd check for.




回答2:


What the error sounds like it is saying is that you are not starting the Zend_Session before data is being sent to the browser. Typically I have something like I pasted below in my bootstrap to start the Zend_Session.

protected function _initSessions()
{
    Zend_Session::start();
}

That should get you going in the Zend Framework. If you are using this tool outside the framework just use that inner line of code near the top of your PHP scripts before you echo or print anything.

Hope that helps!




回答3:


I found a similar post on this problem:

PHPUnit output causing Zend_Session exceptions

I had the same problem but the solution that was proposed on the previous link did not worked for me, so after some testing I found that there is a variable on the class Zend_Session

 /**
 * Whether or not Zend_Session is being used with unit tests
 *
 * @internal
 * @var bool
 */
public static $_unitTestEnabled = false;

If you set this variable to "true" the phpunit execution works without any problem. At least that is what happened in my case.

Hope this help




回答4:


I encountered same problem and my issue was that I was echoing a STRICT warning that was being put to the screen.

A good way to debug this is issue is the use ob_start() and ob_get_contents();

Hope it helps




回答5:


For some reason, I was trying to set some session variable in my index.php file after the application had finished bootstrapping. Not sure why this didn't cause a problem before, but when I removed this from my index.php, it worked.

$userSession = new Zend_Session_Namespace('Auth');
$userSession->forcePasswordChange = false;



回答6:


Possible the session is started through your application.ini or php.ini (session autostart) ? If you use an ide try to search for "spaces".

Sometimes searching for " " (with spaces) helps.




回答7:


I had the same error message. But only for one action. Problem has been caused by these two lines in controller's action:

$customersModel = new Default_Model_DbTable_Customers();
$this->view->customers = $customersModel->fetchAll();

In my database were more than 6000 customers. So it is not good to push a lot of records to view.



来源:https://stackoverflow.com/questions/3928597/zend-session-session-must-be-started-before-any-output-has-been-sent-to-the-bro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!