I want to know how to use PHP session variable in zend framework
here is my code so far :-
public function loginAction()
{
$this->view->title = 'Login';
if(Zend_Auth::getInstance()->hasIdentity()){
$this->_redirect('index/index');
}
$request = $this->getRequest();
$form = new Default_Form_LoginForm();
if($request->isPost()){
if($form->isValid($this->_request->getPost())){
$authAdapter = $this->getAuthAdapter();
$username = $form->getValue('username');
$password = $form->getValue('password');
$authAdapter->setIdentity($username)
->setCredential($password);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if($result->isValid()){
$identity = $authAdapter->getResultRowObject();
print_r($authAdapter->getResultRowObject());
$authStorage = $auth->getStorage();
$authStorage->write($identity);
echo $authAdapter->getIdentity() . "\n\n";
// $this->_redirect('index/index');
} else {
$this->view->errorMessage = "User name or password is wrong.";
}
}
}
$this->view->form = $form;
}
now i want to store username in session and i want to use in some other page like
echo "welcome," .$this->username; what i can do ?
Instead of writing
$identityto$authStorage, you can store a custom object or a model.Here is an example:
Now in your login controller you can do:
Typically, I have an account object that I also store in the UserSession object, and make easy access to the username and userId via public properties.
Now at any time you can get the object:
Just don’t forget to make sure it is a Application_Model_UserSession.