How to use PHP session variable in zend framework

I want to know how to use PHP session variable in zend framework

here is my code: -

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 save the username in the session, and I want to use it on another page, for example

echo "welcome," .$this->username; What can I do?

+6
source share
3 answers

Instead of writing $identity to $authStorage you can save a custom object or model.

Here is an example:

 <?php class Application_Model_UserSession implements Zend_Acl_Role_Interface { public $userId; public $username; /** @var array */ protected $_data; public function __construct($userId, $username) { $this->userId = $userId; $this->username = $username; } public function __set($name, $value) { $this->_data[$name] = $value; } public function __get($name) { if (array_key_exists($name, $this->_data)) { return $this->_data[$name]; } else { return null; } } public function updateStorage() { $auth = Zend_Auth::getInstance(); $auth->getStorage()->write($this); } public function getRoleId() { // TODO: implement $role = 'guest'; return $role; } public function __isset($name) { return isset($this->_data[$name]); } public function __unset($name) { unset($this->_data[$name]); } } 

Now in your login controller you can do:

 if($result->isValid()){ $identity = new Application_Model_UserSession(0, $username); // 0 for userid // You can also store other data in the session, eg: $identity->account = new Account_Model($authAdapter->getResultRowObject()); $identity->updateStorage(); // update Zend_Auth identity with the UserSession object 

Typically, I have an account object, which I also store in the UserSession object, and provides easy access to the username and userId through public properties.

Now you can get an object at any time:

 $identity = Zend_Auth::getInstance()->getIdentity(); // Application_Model_UserSession 

Just remember to make sure this is Application_Model_UserSession.

+6
source

Although you can create an object of the Zend_Session class, however, I would recommend the Zend_Session_Namespace object instead. You can create a session as:

 $sess = new Zend_Session_Namespace('MyNamespace');
$sess = new Zend_Session_Namespace('MyNamespace'); 

If dont pass, Zend Session Namespace assigns the string "default" to its name. To save the values, you will need to do the following:

 $sess->username = 'you_name';
$sess->username = 'you_name'; 

Later in your code, you will need to do the following to extract the value from the session:

 $session = new Zend_Session_Namespace('MyNamespace'); $userName = $sess->username;
$session = new Zend_Session_Namespace('MyNamespace'); $userName = $sess->username; 

Hope this helps

+4
source

In this case, it should be as simple as continuing to add data to your $ authStorage:

 $authStorage = $auth->getStorage(); $authStorage->write($identity); $authStorage->write($username); 

later in another action or controller, you can use Zend_Auth :: getStorage to call your data or use Zend_Session_Namespace.

  $authStorage = $auth->getStorage(); $authStorage->read($identity); $authStorage->read($username); 

or

 $session = new Zend_Session_Namespace('Zend_Auth'); //Zend_Auth uses Zend_Session_Namespace for storage $username = $session->username; 
+4
source

Source: https://habr.com/ru/post/907195/


All Articles