8

Hi: I am using the latest version of Zend Framework (1.9.3PL1). I set the following in my .ini

; Bootstrap session resources
resources.session.save_path = APPLICATION_PATH "/../data/sessions"
resources.session.use_only_cookies = true
resources.session.remember_me_seconds = 864000

Next I want to initialize my session in my bootstrapper:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initSession()
    {
        // What goes here!?
    }
}

My question is, what goes in the initSession function? What should it return, if anything?

Furthermore, if i just start a session in there, it does not recognize the .ini configuration (e.g., the save_path is unchanged). However, if you move the start to a controller, the .ini configuration is recognized.

EDIT: A possible solution is:

protected function _initSession()
{
    // Based on http://framework.zend.com/issues/browse/ZF-6651
    $session = $this->getPluginResource('session'); 
    $session->init(); 
    Zend_Session::start();
}
0

3 Answers 3

11

If you use the resources.session.*-options in your application configuration you must not have a _initSession() method in your bootstrap as these method will override the execution of the plugin resource session (Zend_Application_Resource_Session). The sole exitance of the resources.session.*-options in the configuration file will make sure that the session will be initialized according to your options.

Please read Zend_Application, Theory of Operation for a detailed discussion about the so-called resource methods and the resource plugins.

7

Stefan is quite right, you are overriding the default session resource which makes use of those application options.

If you want to define your own _initSession() method and still access those options use something like:

protected function _initSession() 
{
    $options = $this->getOptions();
    $sessionOptions = array(
        'save_path' => $options['resources']['session']['save_path']
    );    
    Zend_Session::setOptions($options);
    Zend_Session::start();
}
1
  • Some mistake: must be $sessionOptions = array( 'save_path' => $options['session']['save_path'] ); and Zend_Session::setOptions($sessionOptions);
    – Wizard
    Jan 27, 2014 at 8:00
3
    protected function _initSession()
{
    $config = array();
    $config['db'] = array('adapter'=>'PDO_SQLITE',
                     'params' => array('dbname'=> ROOT.'/data/tmp.db3')

                    );
    $config['SaveHandler'] = array(
        'name'    => 'sessions', //table name as per Zend_Db_Table
        'primary' => array(
            'id',   //the sessionID given by PHP
            'path', //session.save_path
            'name', //session name
        ),
        'primaryAssignment' => array(
            //you must tell the save handler which columns you
            //are using as the primary key. ORDER IS IMPORTANT
            'sessionId', //first column of the primary key is of the sessionID
            'sessionSavePath', //second column of the primary key is the save path
            'sessionName', //third column of the primary key is the session name
        ),
        'modifiedColumn' => 'modified', //time the session should expire
        'dataColumn'     => 'data',     //serialized data
        'lifetimeColumn' => 'lifetime', //end of life for a specific record
    );

    $config['lifetime'] = 60*60*24*30;

    $config['options'] = array (
                          'bug_compat_42' => '',
                          'bug_compat_warn' => '',
                          'cache_expire' => '180',
                          'cache_limiter' => 'nocache',
                          'cookie_domain' => '',
                          'cookie_httponly' => '',
                          'cookie_lifetime' => $config['lifetime'],
                          'cookie_path' => '/',
                          'cookie_secure' => '0',
                          'entropy_file' => '',
                          'entropy_length' => '0',
                          'gc_divisor' => '1000',
                          'gc_maxlifetime' => '1440',
                          'gc_probability' => '1',
                          'hash_bits_per_character' => '5',
                          'hash_function' => '0',
                          'name' => 'TaMeR_SESSID',
                          'referer_check' => '',
                          'save_handler' => 'user',
                          'save_path' => '',
                          'serialize_handler' => 'php',
                          'use_cookies' => '1',
                          'use_only_cookies' => 'on',
                          'use_trans_sid' => '0',
                          'strict' => false,
                          'remember_me_seconds' => $config['lifetime'],
                          'throw_startup_exceptions' => true,
    );

    $db = Zend_Db::factory($config['db']['adapter'], $config['db']['params']);
    if( ! in_array('sessions', $db->listTables())) {
        $sql = "CREATE TABLE sessions (";
        $sql .=     "id TEXT, ";
        $sql .=     "path TEXT, ";
        $sql .=     "name TEXT DEFAULT '', ";
        $sql .=     "modified INTEGER, ";
        $sql .=     "lifetime INTEGER, ";
        $sql .=     "data TEXT, ";
        $sql .=     "PRIMARY KEY (id, path, name)";
        $sql .= ");";
        $db->exec($sql);
    }
    Zend_Db_Table_Abstract::setDefaultAdapter($db);
    Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config['SaveHandler']));
    Zend_Session::setOptions($config['options']);
    Zend_Session::start();
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.