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
Zend_Session::rememberMe(60*60*24*7);
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.
Zend_Session::forgetMe();
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
Zend_Session::destroy(false);
To completely logout you will need to do the following
Zend_Session::destroy(true);
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
Zend_Session::namespaceIsset(’MyNamespace’);
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.
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.
ReplyDeleteI 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
thank you for your explanation about zend session.
ReplyDeleteHowever. 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?
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
ReplyDeleteGot 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.
ReplyDeleteAny ideas? Is hasIdentiy() stricter than "normal" functions??
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 :)
ReplyDeleteplease check the spelling of FRAMEWORK in this page didnt expect this "framewrork"
ReplyDeleteHi folks,
ReplyDeleteTo 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 ?
Thanks
Regards,
Sachin Pethani
www.letsnurture.com
wuld u give login session example tutorial....??
ReplyDeletePlz....
hello
ReplyDeleteHi,
ReplyDeleteMay be a typo in your article:
Its sessionExists() and not sessionExist()
Thought I should point it out.
Thanks
i want to update any record using session so plz help
ReplyDeletehow to make session in login time pass the user id
really good work...
ReplyDeleteappreciate... helpfull
rubbish! what if i use singleton pattern and only one session name space is available? You must use getIterator, keep in mind.
ReplyDeleteThanks..........
ReplyDeletethanks so much.
ReplyDeleteThanks for this nice article..
ReplyDeleteBut 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 ?.
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.
ReplyDeleteThanks in advance.
If i am setting my session using this code: $session = new Zend_Session_Namespace('User\Controller');
ReplyDeletethen how could i unset the namespace in ZF2.
Zend_Session:: namespaceUnset(’MyNamespace’);
DeleteThanx........... :D
ReplyDeletethanks... really help :D
ReplyDeleteHi,
ReplyDeletegood information
Web application development services will successfully change the execution of your online business So, hire our skilled Ecommerce website company to get customized applications for your business.
Best Website Designing and Development Company
"
Ecommerce Website Development Company"
Zend Framework