Thursday, July 2, 2009

Zend Framework Session usage and examples

Session handling was one of the boring topic in plan php atleast for me, but in Zend Framework it really shines. If you like to work with Object oriented programming, you will definitely become fan of it.
In this article I’m going to discuss some useful techniques of using Zend Framework Session and Session namespace.
Keep in mind that Both Zend_Session and Zend_Session_Namespace extends abstract class Zend_Session_Abstract. So both inherit methods avaliable in Zend_Session_Abstract automatically.
If you want to go under the hood, you can open Zend/Session/Abstract.php and have a look at the functions available.
Instead of delve into the function available, I’d rather discuss some useful techniques.
Although you can create Zend_Session class object, however I would recommend Zend_Session_Namespace object instead. You can instantiate session as
$sess = new Zend_Session_Namespace(’MyNamespace’);

Here the argument is optional, however it is better to pass it. If don’t pass, Zend Session Namespace will assign its name a string “default”.
To store values, you will need to do the following.
$sess->username = ‘you_name’;

Later in your code, you will need to do the following to retrieve value from the session.
    $session = new Zend_Session_Namespace(’MyNamespace’);

$userName = $sess->username;

This was a simple example of how to assign values to session and later retrieve. Now I’m going to discuss some of its important method.
To check whether or not session exist, write the following.
    If (Zend_Session::sessionExist()) {

// do something.


sessionExist() is static method available in Zend_Session.

Another very important method is rememberMe() available in Zend_Session. This is used to make your session persistent.
Its syntax is

The number passed as an argument specify for how long the session will be available. What I have done means that session will be there for a week.
To destroy this you will need to call the following function on the logout of your application.

Another method
destroy(bool,bool), take two Boolean arguments. This destroy the entire persistent data. Keep in mind that Zend_Session_Namespace data will not be destroyed. You can retrieve that data even after you call

To completely logout you will need to do the following

We now passed true instead of false.
The above methods were anyhow related to Zend_Session class. To check the individual namespaces you can use the following function

This will check whether the specified namespace has been set.
To unset the particular namespace, you will need to write the following.
Zend_Session:: namespaceUnset(’MyNamespace’);

There are plenty of other options available. But I don’t thing you will mostly need those.

If you do need extra functionality, don’t hesitate to open Zend_Session_Abstract.php, Zend_Session_Nampspace.php, and Zend_Session.php.
If you still finding it difficult to understand, do post your comments. I’d be glad to give you solution to your problem.


  1. Hello Faheem... excellent post.. thanks very much. This was the best reference i could get for usage of zend session and it works pretty good.

    I have one question though. In your example of the session table, you have used a 4 column table,
    id char(32),
    modified int,
    lifetime int,
    data text,

    If i want to store the loginid of the user with whom the session is associated in the database table, like
    logind.. how can that be done with Zend_Session.

    I was assuming that in the config we would pass to Zend_Session_SaveHandler_DbTable, would mention the column name, but was not sure whether to make that also a joint primary key ?

    Also how would i populate that loginid field within the controller so that it is saved to the table ?

    thanks for any suggestions you may have

    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from JQuery Training in Chennai . or learn thru JQuery Training . or learn thru ES6 Online Training. Nowadays JavaScript has tons of job opportunities on various vertical industry.

  2. thank you for your explanation about zend session.
    However. I try to use RememberMe feature for 1 week and the session is still there for 1 week. But the problem is: the HasIdentity is lost in a couple of hours so the status is logged out again.. do you know why it's happening?

  3. This is excellent article to start of with session handling using zend framework. i need a help from you, i am designing a zend form since it is very convinent to provide validation check using zend form but i am getting very much trouble in changing look and feel of form using decorators also i am not get concept dd and dt tags that zend uses in while creating zend form. please write an simple article explaining decorators

  4. Got the same problem as Riki. My cookie expires in two weeks but the i am logged out after the browser is closed for aboute 15 minutes. After 2-5 minutes everything works as expected.

    Any ideas? Is hasIdentiy() stricter than "normal" functions??

  5. I don't agree with this post since you create or initialize the same class. the object will return NULL because the object will be overwritten :)

  6. please check the spelling of FRAMEWORK in this page didnt expect this "framewrork"

  7. Hi folks,

    To achieve the "Remember Me" functionality in Zend framework, is it right practice to use "Zend_Session::rememberMe".

    As far as I guess, it keeps the session alive on serverside for specified time. So it will increase the burden on server side. Am I correct?

    Is there any other way to achieve "Remember Me" functionality in zend framework ?


    Sachin Pethani

  8. wuld u give login session example tutorial....??

  9. Hi,

    May be a typo in your article:

    Its sessionExists() and not sessionExist()

    Thought I should point it out.


  10. i want to update any record using session so plz help
    how to make session in login time pass the user id

  11. really good work...
    appreciate... helpfull

  12. rubbish! what if i use singleton pattern and only one session name space is available? You must use getIterator, keep in mind.

  13. This is a nice article..
    Its very easy to understand ..
    And this article is using to learn something about it..

    c#,, php tutorial

    Thanks a lot..!

  14. Thanks for this nice article..

    But i want to know how to denay the user to open a new session, for example, if i have "zend" as username and "pass" as password, what is the metod to do to not allow my freind to open a new session with the same parameters while my session is opened ?.

  15. Whenever i am using Zend_Session in my code i am getting error something like this: Fatal error: Class 'User\Controller\Zend_Session_Namespace' not found in D:\wamp\www\online_food\module\User\src\User\Controller\UserController.php, kindly provide me solution for this i am struggling to solve this issue.

    Thanks in advance.

  16. If i am setting my session using this code: $session = new Zend_Session_Namespace('User\Controller');

    then how could i unset the namespace in ZF2.

    1. Zend_Session:: namespaceUnset(’MyNamespace’);

  17. Thanx........... :D

  18. php code for photo tagging with demo and database