Zend: Fetching row from session db table after generating session id
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 t开发者_StackOverflowhe 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!
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.
You need to "detach/release" your session before you can do anything with it. You can do so by calling : Zend_Session::writeClose(); Then you should be able to fetch/update rows as desired. Just bear in mind, it will make your session read-only, so just ensure you won't need to write to it after it's detached.
精彩评论