“session has already been started…” exception in Zend Framework application

后端 未结 12 781
囚心锁ツ
囚心锁ツ 2020-12-31 08:18

I get this error when trying to load a Zend Framework application:

Fatal error: Uncaught exception \'Zend_Session_Exception\' with message \'sessi

相关标签:
12条回答
  • 2020-12-31 09:17

    It's what it says it is. Zend_Auth tries to start a new session, since Zend_Session::start() has not yet been called.

    The problem is that Zend_Session::start() has to be called before a session is started. But, since session.autostart is 0 (btw this is in php.ini not .htaccess), you have probably written session_start(); somewhere. You're not allowed to do that, since ZF wishes to have full control over sessions, i.e. you shouldn't access the global session variable directly.

    To solve it, search your code files for session_start() and either

    1. remove all occurences but one. To notice if it's already been started, set error_reporting(E_ALL|E_STRICT);
    2. replace it with Zend_Session::start(); at all places

    If you can't find all occurrences, find the one session_start(); that bothers your Zend_Auth::getInstance()->hasIdentity() and solve the problem quick n' dirty with the following snippet

    try {
        Zend_Session::start();
    } catch(Zend_Session_Exception $e) {
        session_start();
    }
    

    If you're using ZF in your whole application, I would go with 2)

    0 讨论(0)
  • 2020-12-31 09:18

    In case this of any use, I cleared this error by taking out session related lines from my application/config/application.ini

    ;resources.session.save_path = APPLICATION_PATH "/../data/session"
    ;resources.session.use_only_cookies = true
    ;resources.session.remember_me_seconds = 3600
    

    Thanks to chelmertz for the insight into the cause of the problem.

    0 讨论(0)
  • 2020-12-31 09:21

    I'd like to draw your attention to the garbage collection problem, solved here Issues with PHP 5.3 and sessions folder or http://somethingemporium.com/2007/06/obscure-error-with-php5-on-debian-ubuntu-session-phpini-garbage.

    After fixing the 'session has already been started' I encountered the GC bug. I suspect that the GC bug might be the root cause for the session bug at least in some instances. So far I have not had enough time to investigate thoroughly, but please comment if GC and session bug are related in your case as well.

    0 讨论(0)
  • 2020-12-31 09:23

    There are 3 major reasons that produce this issue:

    1. session.auto_start should be set to 0 or off. You can check it by placing phpinfo(); in any file and try to access it in browser, then search auto_start is it 0 or off. If not then set it off or 0.
    2. Check session path session.save_path in configuration at server. If with default configuration it's displaying error 'session has already been started by session.auto-start or session_start()', set it to '/temp'
    3. May be you have used session_start() in your code before zend session initialization.

    In most cases the 2nd option is the reason.

    0 讨论(0)
  • 2020-12-31 09:24

    I had the same error. On local machine, everything worked fine. On server not. My solution was to put Zend_Session::start(); in the index.php before running bootstrap. So that it looks like this:

    <?php
    // Define path to application directory
    defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
    
    // Define application environment
    defined('APPLICATION_ENV')
        || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
    
    // Ensure library/ is on include_path
    set_include_path(implode(PATH_SEPARATOR, array(
        realpath(APPLICATION_PATH . '/../library'),
        get_include_path(),
    )));
    
    /** Zend_Application */
    require_once 'Zend/Application.php';  
    
    // Create application, bootstrap, and run
    $application = new Zend_Application(
        APPLICATION_ENV, 
        APPLICATION_PATH . '/configs/application.ini'
    );
    
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
    
    Zend_Session::start();
    
    $application->bootstrap()->run();
    
    0 讨论(0)
  • 2020-12-31 09:24

    There is an issue opened for this problem:

    #25 "session has already been started by session.auto-start or session_start()" error message is misleading

    0 讨论(0)
提交回复
热议问题