views:

148

answers:

1

Hi,

I'm trying to update the session table used by Zend_Session_SaveHandler_DbTable directly after authenticating the user and writing the session to the DB. But I can neither update nor fetch the newly inserted row, even though the session id I use to check (Zend_Session::getId()) is valid and the row is indeed inserted into the table. Upon fetching all session ids (on the same request) the one I newly inserted is missing from the results. It does appear in the results if I fetch it with something else. I've checked whether it is a problem with transactions and that does not seem to be the problem - there is no active transaction when I'm fetching the results. I've also tried fetching a few seconds after writing using sleep(), which doesn't help.

$auth->getStorage()->write($ident);
//sleep(1)
$update = $this->db->update('session', array('uid' => $ident->user_id), 'id='.$this->db->quote(Zend_Session::getId()));
$qload = 'SELECT id FROM session';
$load = $this->db->fetchAll($qload);
echo $qload;
print_r($load);

$update fails.
$load doesn't contain the row that was written with $auth->getStorage()->write($identity). $qload does contain the correct query - copying it to somewhere else leads to the expected result, that is the inserted row is included in the results.

Database used is MySQL - InnoDB.

If someone knows how to directly fix this (i.e. on the same request, not doing something like updating after redirecting to another page) without modifying Zend_Session_SaveHandler_DbTable: Thank you very much!

A: 

This is not an issue with the Zend Framework but an issue with how Sessions work in PHP. In your case, when you create a new Session, the new Session ID is generated but the Session only exists in memory until after the script exits.

Source: PHP Session Handling, Notes

You could perhaps handle this on the same request by adding a different table to hold session metadata, using the generated Session ID as a primary key (or component of the primary key). If you go this route, extend Zend_Session_SaveHandler_DbTable and overwrite the destroy() method to keep your metadata table clean when Sessions expire and are garbage collected.

yenta