Jump to content
Stef's Super Nerd Coding Community
Sign in to follow this  
phptek

Zend Sessions

Recommended Posts

Can you please post an example of setting the username of the user into a session and displaying it on the page as a greeting as "Hello username".

Can you please use an easy way to do it using the latest zend framework in controller class?

 

Thanks a lot.

 

Sam

Share this post


Link to post
Share on other sites

I can't tell you how to do it within Zend... but I can't see why you couldn't use regular PHP within Zend?

 

session_start();

$_SESSION['username'] = 'username'; // set session

 

echo $_SESSION['username']; // display username

 

If Zend has it's own unique way of handling sessions though, this may not be helpful. In that case, hopefully another user can help you out. This might also help you: http://zendgeek.blogspot.com/2009/07/zend-framework-session-usage-and.html

Share this post


Link to post
Share on other sites

I can't tell you how to do it within Zend... but I can't see why you couldn't use regular PHP within Zend?

 

session_start();

$_SESSION['username'] = 'username'; // set session

 

echo $_SESSION['username']; // display username

 

If Zend has it's own unique way of handling sessions though, this may not be helpful. In that case, hopefully another user can help you out. This might also help you: http://zendgeek.blogspot.com/2009/07/zend-framework-session-usage-and.html

 

Thank you, Ben. I am familiar with regular php syntax. I was wondering if there are some classes from which i can use some methods to set sessions in Zend framework. I will check out the link you posted as well. BTW, the videos from killerphp are what got me into Zend, and now I'm loving it.

Thanks for all you do to the community.

Happy Thanksgiving. - Sam

Share this post


Link to post
Share on other sites

Set into Registry:

Zend_Registry::set($index, $value);

 

to retrieve that you've store:

$session = Zend_Registry::get($index);

//value is now stored into $session

 

you can even store / pass an object and work on it if you have to:

$session = Zend_Registry::get($index)->objectMethod();

 

Usually a good idea to check if your registry entry exists before tryig to access:

if (Zend_Registry::isRegistered($index)) {
...
}

Share this post


Link to post
Share on other sites

realized after i had some lunch that my post might have confused you since i used Zend_Registry examples

 

Zend_Registry and Zend_Session_Namespace are different obviously. If you must use sessions then below are some examples as well.

 

start or call your session

$session = new Zend_Session_Namespace($name);

 

add something to your session

$session->index = $value;

 

call that value somewhere else in your code

$session = new Zend_Session_Namespace($name);
//make sure its set
if(!isset($session->index)){
//set it
} else {
//do what ya need to with its value
}

 

Registry will be cleared with every new Bootstrap request, thus you'd need to ensure your values are setup to load into it with every request, so it may not be useful with whatever you are planning to do. I tend to use them more often than Zend_Session_Namespace however.

Share this post


Link to post
Share on other sites

realized after i had some lunch that my post might have confused you since i used Zend_Registry examples

 

Zend_Registry and Zend_Session_Namespace are different obviously. If you must use sessions then below are some examples as well.

 

start or call your session

$session = new Zend_Session_Namespace($name);

 

add something to your session

$session->index = $value;

 

call that value somewhere else in your code

$session = new Zend_Session_Namespace($name);
//make sure its set
if(!isset($session->index)){
//set it
} else {
//do what ya need to with its value
}

 

Registry will be cleared with every new Bootstrap request, thus you'd need to ensure your values are setup to load into it with every request, so it may not be useful with whatever you are planning to do. I tend to use them more often than Zend_Session_Namespace however.

 

Thank you very much J. That really helped.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...