Zend Framework: Storing Session in Database
Storing Session in Database is one of the ways to handle session data in the environment with multiple web servers. Even so the documentation about Zend_Session_SaveHandler_DbTable and Zend_Application_Resource_Session is available you still have to know a little bit more in order to use them. I ‘m going to reveal this little thing. Please let me know in comments if this post was helpful.
Summary
- Assumptions
- Create Database Table
- Initialize a session SaveHandler in
application.ini - Start session in bootstrap class
- Saving and Retrieving data from session
- Source code
Assumptions
Your Zend Framework version is 1.10
It’s necessary for your application to store session in database
Create Database Table
The table which stores session data must at least have four columns: the session id which by default is a string of length 32; modified, which is the unix timestamp of the last modified date; lifetime, which is the lifetime of the session (modified + lifetime > time();); and data, which is the serialized data stored in the session
CREATE TABLE `session` (
`id` char(32),
`modified` int,
`lifetime` int,
`data` text,
PRIMARY KEY (`id`)
);
Initialize a session SaveHandler in application.ini
Important: You need to define database resource before session resource.
# Database resources.db.adapter = "Pdo_Mysql" resources.db.params.host = "localhost" resources.db.params.username = "webuser" resources.db.params.password = "XXXX" resources.db.params.dbname = "test" resources.db.isDefaultTableAdapter = true # Session resources.session.use_only_cookies = true resources.session.gc_maxlifetime = 864000 resources.session.remember_me_seconds = 864000 resources.session.saveHandler.class = "Zend_Session_SaveHandler_DbTable" resources.session.saveHandler.options.name = "session" resources.session.saveHandler.options.primary = "id" resources.session.saveHandler.options.modifiedColumn = "modified" resources.session.saveHandler.options.dataColumn = "data" resources.session.saveHandler.options.lifetimeColumn = "lifetime"
Start session in bootstrap class
In order to start session you need to add the following method to your Bootstrap class
/**
* Start session
*/
public function _initCoreSession()
{
$this->bootstrap('db');
$this->bootstrap('session');
Zend_Session::start();
}
Saving and Retrieving data from session
You can use the following code to set and retrieve value form session.
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
// default namespace
$namespace = new Zend_Session_Namespace();
$namespace->foo = 100;
// mySpace namespace
$namespace = new Zend_Session_Namespace('mySpace');
$namespace->foo = 100;
// action body
}
}
Source code
https://github.com/denisura/zfSessionDatabase
Related posts:
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
thanks for posting this article. I tried to integrate Zend_Session with using zend.application.available-resources.html and learning.multiuser.sessions.html pages on ZF’s manual . I was stuck because I couldn’t find the minimum content I have to add to config.ini .I got them from this post and it works,Thanks again. but we don’t need to add a resource method to bootstrap, do we? .I saw also this when searching, and it also proves it.sorry about my bad english and thanks again for post this
Could you send me your log files?
Extremely sorry, I didn’t see that they have use “resources.session.*” to start session. I tried it but it not worked. then I tried using a bootstrap method like you said, It worked.Thanks again for writing this.it’s really helpful to me.
Very helpful, thanks!! Fills the gaps in the official documentation nicely
Thanks for your post.
The official document really confused me.
I solved the problem with the help of your post!
[...] regards to moving session from the file level to the database level please check out this article: http://dionysus.uraganov.net/frameworks/zend-framework-storing-session-in-database/. The check makes two assumptions. One there is an “id” field in your table schema that [...]