如何在zend框架中使用PHP会话变量

时间:2012-01-30 03:53:25

标签: php zend-framework

我想知道如何在zend框架中使用PHP会话变量

到目前为止,这是我的代码: -

 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;

     }

现在我想在会话中存储用户名,我想在其他页面中使用

echo "welcome," .$this->username;我能做什么?

3 个答案:

答案 0 :(得分:6)

您可以存储自定义对象或模型,而不是将$identity写入$authStorage

以下是一个例子:

<?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]);
    }
}

现在,您可以在登录控制器中执行以下操作:

if($result->isValid()){
    $identity = new Application_Model_UserSession(0, $username); // 0 for userid

    // You can also store other data in the session, e.g.:
    $identity->account = new Account_Model($authAdapter->getResultRowObject());

    $identity->updateStorage(); // update Zend_Auth identity with the UserSession object

通常,我有一个帐户对象,我也存储在UserSession对象中,并通过公共属性轻松访问用户名和userId。

现在您可以随时获取该对象:

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

请不要忘记确保它是Application_Model_UserSession。

答案 1 :(得分:4)

虽然您可以创建Zend_Session类对象,但我建议使用Zend_Session_Namespace对象。您可以将会话实例化为:


$sess = new Zend_Session_Namespace('MyNamespace');

如果没有通过,Zend会话名称空间将为其名称分配一个字符串“default”。 要存储值,您需要执行以下操作:


$sess->username = 'you_name';

稍后在您的代码中,您需要执行以下操作以从会话中检索值:


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

希望有所帮助

答案 2 :(得分:4)

在这种情况下,它应该像继续将数据添加到$ authStorage一样简单:

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

稍后在另一个操作或控制器中,您可以使用Zend_Auth :: getStorage来调用您的数据或使用Zend_Session_Namespace。

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

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