如何在zend框架中使用PHP会话变量
我想知道如何在 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 ->用户名;
我能做什么?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以存储自定义对象或模型,而不是将
$identity
写入$authStorage
。下面是一个示例:
现在,在您的登录控制器中,您可以执行以下操作:
通常,我有一个帐户对象,我也将其存储在 UserSession 对象中,并通过公共属性轻松访问用户名和 userId。
现在您可以随时获取该对象:
只是不要忘记确保它是 Application_Model_UserSession。
Instead of writing
$identity
to$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.
虽然您可以创建 Zend_Session 类对象,但是我建议改为 Zend_Session_Namespace 对象。您可以将会话实例化为:
如果不通过,Zend 会话命名空间将为其名称指定一个字符串“default”。
要存储值,您需要执行以下操作:
稍后在代码中,您将需要执行以下操作以从会话中检索值:
希望它有帮助
Although you can create Zend_Session class object, however I would recommend Zend_Session_Namespace object instead. You can instantiate session as:
If don’t pass, Zend Session Namespace will assign its name a string “default”.
To store values, you will need to do the following:
Later in your code, you will need to do the following to retrieve value from the session:
Hope it helps
在这种情况下,它应该像继续将数据添加到 $authStorage:
一样简单,
稍后在另一个操作或控制器中,您可以使用 Zend_Auth::getStorage 来调用您的数据或使用 Zend_Session_Namespace。
或者
In this case it should be as simple as continuing to add the data to your $authStorage:
later in another action or controller you can either use Zend_Auth::getStorage to recall your data or use Zend_Session_Namespace.
or