Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
        }
    }
}
like image 302
Andrew Avatar asked Dec 04 '22 11:12

Andrew


2 Answers

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.

like image 91
Maltronic Avatar answered Apr 19 '23 10:04

Maltronic


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!

like image 36
gokujou Avatar answered Apr 19 '23 11:04

gokujou