update:
zend framework> = 2.2, this question no longer exists.
Old answer:
I faced the same problem while saving the session in the database, I wrote the initDbSession function inside the base class of the module \module\Application\Module.php
class Module { public function onBootstrap(MvcEvent $e) { $e->getApplication()->getServiceManager()->get('translator'); $eventManager = $e->getApplication()->getEventManager(); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->attach($eventManager); // session start from here $this->initDbSession( $e ); } /** * Store session into database * * @param type $e */ private function initDbSession( MvcEvent $e ) { // grab the config array $serviceManager = $e->getApplication()->getServiceManager(); $config = $serviceManager->get('config'); $dbAdapter = $serviceManager->get('Zend\Db\Adapter\Adapter'); /* some how this not works for me $sessionOptions = new \Zend\Session\SaveHandler\DbTableGatewayOptions( null ); $sessionTableGateway = new \Zend\Db\TableGateway\TableGateway('session', $dbAdapter); $saveHandler = new \Zend\Session\SaveHandler\DbTableGateway($sessionTableGateway, $sessionOptions); */ /* I written my own save handler , I am using mysql as database */ $saveHandler = new \My\Session\SaveHandler\Mysql( $config['db'] ); $sessionConfig = new \Zend\Session\Config\SessionConfig(); $sessionConfig->setOptions($config['session']); // pass the saveHandler to the sessionManager and start the session $sessionManager = new \Zend\Session\SessionManager( $sessionConfig , NULL, $saveHandler ); $sessionManager->start(); \Zend\Session\Container::setDefaultManager($sessionManager); } // other function goes here ...
Here is my configuration file, which is located in \config\autoload\global.php
return array( 'db' => array( 'driver' => 'Pdo', 'dsn' => 'mysql:dbname=zf2;host=localhost', 'driver_options' => array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'', 'buffer_results' => true ), 'username' => 'root', 'password' => '', 'host' => 'localhost', 'dbname' => 'zf2', ), 'session' => array( 'remember_me_seconds' => 2419200, 'use_cookies' => true, 'cookie_httponly' => true, 'cookie_lifetime' => 2419200, 'gc_maxlifetime' => 2419200, ), 'service_manager' => array( 'factories' => array( 'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory' ) ) );
Mysql table structure
CREATE TABLE `session` ( `id` CHAR(32) NOT NULL DEFAULT '', `name` VARCHAR(255) NOT NULL, `modified` INT(11) NULL DEFAULT NULL, `lifetime` INT(11) NULL DEFAULT NULL, `data` TEXT NULL, PRIMARY KEY (`id`) )COLLATE='utf8_general_ci' ENGINE=InnoDB;
The custom Mysql save handler class is shown below. I wrote this class because the new Zend\Session\SaveHandler\DbTableGateway not working on my server. My custom Mysql session session handler class written in \library\My\Session\SaveHandler\Mysql.php
<?php namespace My\Session\SaveHandler; use Zend\Session\SaveHandler\SaveHandlerInterface; class Mysql implements SaveHandlerInterface { protected $sessionSavePath; protected $sessionName; protected $lifetime; public function __construct( $dbConfig ) { $this->dbconn = mysql_connect( $dbConfig['host'], $dbConfig['username'], $dbConfig['password'] ); if ( $this->dbconn ) { return mysql_select_db($dbConfig['dbname'], $this->dbconn); } } public function open( $savePath, $name ) { $this->sessionSavePath = $savePath; $this->sessionName = $name; $this->lifetime = ini_get('session.gc_maxlifetime'); return true; } public function close() { return mysql_close($this->dbconn); } public function read($id) { $id = mysql_real_escape_string($id); $sql = "SELECT `data` FROM `session` " . "WHERE id = '$id'"; if ( $result = mysql_query($sql, $this->dbconn)) { if ( mysql_num_rows($result) ) { $record = mysql_fetch_assoc($result); return $record['data']; } } return ''; } public function write($id, $data ) { $data = (string) $data ; $dbdata = array( 'modified' => time(), 'data' => mysql_real_escape_string( $data ) , ); $selectSql = "SELECT * FROM session WHERE id = '$id' AND name = '{$this->sessionName}' "; $rs = mysql_query( $selectSql, $this->dbconn ); if ( $rs = mysql_query( $selectSql , $this->dbconn)) { if ( mysql_num_rows($rs) ) { $updateSql = "UPDATE `session` SET `modified`= '".$dbdata['modified'] . "' , `data`= '".$dbdata['data']. "' WHERE id= '$id' AND name = '{$this->sessionName}' "; mysql_query( $updateSql , $this->dbconn ); return true; } } $dbdata['lifetime'] = $this->lifetime; $dbdata['id'] = $id; $dbdata['name'] = $this->sessionName; $insertSql = "INSERT INTO session (". implode(',' , array_keys($dbdata)) .")" ."VALUES ('" . implode("','" , array_values( $dbdata )). "')"; return mysql_query( $insertSql, $this->dbconn); } public function destroy($id) { $sql = sprintf("DELETE FROM `session` WHERE `id` = '%s'", $id); return mysql_query($sql, $this->dbconn); } public function gc( $maxlifetime ) { $sql = sprintf("DELETE FROM `session` WHERE `modified` < '%s'", mysql_real_escape_string(time() - $maxlifetime) ); return mysql_query($sql, $this->dbconn); } }
Which saves the session values ββin the db table. For more information, you can check http://php.net/manual/en/function.session-set-save-handler.php