In this third part about the Zend Framework i am diving into the world of Zends implementation for sessions. Sessions are used to provide a way to save data between different page requests.
The Zend Framework has an easy way of managing sessions by creating a new instance of the Zend_Session_Namespace class. This class provides an easy to use API on the Zend_Session managed superglobal $_SESSION. By default the session data is saved to the filesystem which works perfect for simple applications using a single webserver but will be more problematic when multiple webservers are used all with their own filesystem.
A solution for this problem is saving the session data to a central database which is available to all webservers. The Zend Framework provides a class which can be used for this: Zend_Session_SaveHandler_DbTable which can be configured completely as a resource within the application.ini file.
Using the database as a central source for session data gives some other worries though:
- You have to make sure the database will be available at all time
- Clustering databases for high availability is something not all persons have knowledge on
- Managing the session tables within these databases poses some headaches when changes to the existing table are needed.
Another approach is using a memory based store like memcache, but in this post i would like to use the “new” kid in town called MongoDB, a scalable, high-performance, open source, document-oriented database. For this we are going to write a new session save handler for Zend Framework called GcLib_Zend_Session_SaveHandler_MongoDb. The source of this class can be downloaded here.