The Zend_Session component is both a session manager as well as
an API for storing data into a session object for long-term
persistence. The Zend_Session API is for
managing the options and behavior of a session, like options, starting and stopping a
session, whereas Zend_Session_Namespace is the actual object used
to store data.
While its generally good practice to start a session inside a bootstrap process, this
is generally not necessary as all sessions will be automatically started upon the first
creation of a Zend_Session_Namespace object.
Zend_Application is capable of configuring
Zend_Session for you as part of the
Zend_Application_Resource system. To use this, assuming your
project uses Zend_Application to bootstrap, you would add the
following code to your application.ini file:
resources.session.save_path = APPLICATION_PATH "/../data/session" resources.session.use_only_cookies = true resources.session.remember_me_seconds = 864000
As you can see, the options passed in are the same options that you'd expect to find in the ext/session extension in PHP. Those options setup the path to the session files where data will be stored within the project. Since INI files can additionally use constants, the above will use the APPLICATION_PATH constant and relatively point to a data session directory.
Most Zend Framework components that use sessions need nothing more to use
Zend_Session. At this point, you an either use a component that
consumes Zend_Session, or start storing your own data inside a
session with Zend_Session_Namespace.
Zend_Session_Namespace is a simple class that proxies data via an
easy to use API into the Zend_Session managed
$_SESSION superglobal. The reason it is called
Zend_Session_Namespace is that it effectively namespaces the data
inside $_SESSION, thus allowing multiple components and objects to safely store and
retrieve data. In the following code, we'll explore how to build a simple session
incrementing counter, starting at 1000 and resetting itself after 1999.
$mysession = new Zend_Session_Namespace('mysession');
if (!isset($mysession->counter)) {
$mysession->counter = 1000;
} else {
$mysession->counter++;
}
if ($mysession->counter > 1999) {
unset($mysession->counter);
}
As you can see above, the session namespace object uses the magic __get, __set, __isset, and __unset to allow you to seamlessly and fluently interact with the session. The information stored in the above example is stored at $_SESSION['mysession']['counter'].




