Jump to content

Recommended Posts

Posted

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

Posted

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

Posted

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

Posted

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)) {
...
}

Posted

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.

Posted

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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...