Zend_Session_SaveHandler_DbTable is wiping the Session with every refresh?

841 views Asked by At

I'm basically encountering the same problem as the poster in this question. My database is initialized properly. I've tried doing the initialization of both the database and the session SaveHandler in the application.ini and in the Bootstrap. Same result no matter how I do it.

Here's what the application.ini initialization looks like:

resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "uname"
resources.db.params.password = "******"
resources.db.params.dbname = "dbname"

resources.session.saveHandler.class = "Zend_Session_SaveHandler_DbTable"
resources.session.saveHandler.options.name = "sessions"
resources.session.saveHandler.options.primary = "sessionID"
resources.session.saveHandler.options.modifiedColumn = "lastModifiedTime"
resources.session.saveHandler.options.dataColumn = "data"
resources.session.saveHandler.options.lifetimeColumn = "lifetime"

And here's what the Bootstrap initialization looked like:

protected function _initSession() {
        $db = Zend_Db::factory('Pdo_Mysql', array(
            'host'        =>'localhost',
            'username'    => 'uname',
            'password'    => '******',
            'dbname'    => 'dbname'
        ));
        Zend_Db_Table_Abstract::setDefaultAdapter($db);


    $sessionConfig = array( 
            'name'           => 'sessions',      
            'primary'        => 'sessionID',   
            'modifiedColumn' => 'lastModifiedTime',     
            'dataColumn'     => 'data',
            'lifetimeColumn' => 'lifetime'
        ); 
        $saveHandler = new Zend_Session_SaveHandler_DbTable($sessionConfig); 
        Zend_Session::setSaveHandler($saveHandler); 
        Zend_Session::start();

}

My sessions database table is defined as follows:

create table sesssions (
    sessionID char(32) primary key not null, 
    lastModifiedTime timestamp, 
    lifetime timestamp, 
    data text
) engine=innodb;

I have a test action that tests this through a very simple one field form that just dumps its contents into the Session. The action looks like this:

public function addAction()
{
    $namespace = new Zend_Session_Namespace();

    $form = new Application_Form_AddToSession();
    $request = $this->getRequest();
    if ($request->isPost()) {
        if ($form->isValid($request->getPost())) {
           $namespace->content = $request->getParam('toAdd');
        }
    }
    $this->view->form = $form; 
}

Here's the form it uses:

class Application_Form_AddToSession extends Zend_Form
{

    public function init()
    {
        $this->setMethod('post');

        $this->addElement('text', 'toAdd', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                array('StringLength', false, array(0, 256)),
            ),
            'required'   => true,
            'label'      => 'Add:',
        ));

        $this->addElement('submit', 'add', array(
            'required' => false,
            'ignore'   => true,
            'label'    => 'Add',
        )); 
    }


}

The view just shows the form.

To test whether or not the value actually went into the session, I use the index action. This is the index action in question:

public function indexAction()
{
    $namespace = new Zend_Session_Namespace();
    echo 'Content: '.$namespace->content.'<br>';
    echo '<pre>'; print_r($_SESSION); echo '</pre>';
}

Now. If I don't have Session saving configured to use Zend_Session_SaveHandler_DbTable, ie, if I don't have session saving configured at all, then this works fine. I enter a value in the form field, go to the index action and have it output back to me. Session works exactly the way it is supposed to.

If I have Zend_Session_SaveHandler_DbTable configured in either the application.ini or the Bootstrap, then when I enter a value into the test field and go to the index action the value is gone. My database table has a row with the proper sessionID and the sessionID matches a cookie in my browser. But there is no other information in the database. data is NULL and both the TIMESTAMP fields are zeroed out.

I've run out of things to try. I've had the Mysql table as a regular table and an InnoDB table. I've tried every permutation of the database and session configuration I can come up with, including giving the db to the configuration array, and initializing one in the Bootstrap and the other in the .ini. I've scoured the web and StackOverflow for clues. I've seen other people post about similar problems, but none of the answers I've found have worked. What haven't I done? What have I screwed up? How can I make it work?

1

There are 1 answers

1
ischenkodv On BEST ANSWER

The problem is that you defined lastModifiedTime and lifetime columns as timestamp. They should be INT instead:

CREATE TABLE  `sessions` (
`sessionID` char(32) NOT NULL,
`lastModifiedTime` INT,
`lifetime` INT,
`data` text,
PRIMARY KEY (`sessionID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

After this small modification it should work.