Zend 会话和 zend 身份验证
我已经通过 zend auth 创建了一个登录系统,这是代码
// userAuthentication
public function authAction(){
$request = $this->getRequest();
$registry = Zend_Registry::getInstance();
$auth = Zend_Auth::getInstance();
$DB = $registry['DB'];
$authAdapter = new Zend_Auth_Adapter_DbTable($DB);
$authAdapter->setTableName('user')
->setIdentityColumn('user_name')
->setCredentialColumn('user_password');
$username = $request->getParam('username');
$password = $request->getParam('password');
$authAdapter->setIdentity($username);
$authAdapter->setCredential($password);
$result = $auth->authenticate($authAdapter);
if($result->isValid()){
$data = $authAdapter->getResultRowObject(null,'password');
$auth->getStorage()->write($data);
$this->_redirect('/login/controlpannel');
}else{
$this->_redirect('/login/login');
}
}
,现在工作正常。用户(表)中有 user_id(列),其中也有用户名和密码。我需要从此表中获取特定的 user_id,只需登录并将其放入会话中,
$user_session = new Zend_Session_Namespace('user_session');
$user_session->username = $username;
$user_id->user_id = $user_id;
以便我可以针对此 $user_id 查询一些信息并将结果传递到视图(名称)控制面板
I have made a login system through zend auth here is the code
// userAuthentication
public function authAction(){
$request = $this->getRequest();
$registry = Zend_Registry::getInstance();
$auth = Zend_Auth::getInstance();
$DB = $registry['DB'];
$authAdapter = new Zend_Auth_Adapter_DbTable($DB);
$authAdapter->setTableName('user')
->setIdentityColumn('user_name')
->setCredentialColumn('user_password');
$username = $request->getParam('username');
$password = $request->getParam('password');
$authAdapter->setIdentity($username);
$authAdapter->setCredential($password);
$result = $auth->authenticate($authAdapter);
if($result->isValid()){
$data = $authAdapter->getResultRowObject(null,'password');
$auth->getStorage()->write($data);
$this->_redirect('/login/controlpannel');
}else{
$this->_redirect('/login/login');
}
}
This work fine now. There is user_id (column) in user (table) where there are username and password too. I need to get that specific user_id from this table which just login and put it in session through
$user_session = new Zend_Session_Namespace('user_session');
$user_session->username = $username;
$user_id->user_id = $user_id;
so that I can query some info against this $user_id and pass the result into view (name) controlpanel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从存储中获取用户 ID:
Get user id from storage :
虽然这个问题已经得到解答,但我倾向于比
getStorage()->read()
链更频繁地使用getIdentity()
函数。下面的例子。While this was already answered, I tend to use the
getIdentity()
function more frequently than thegetStorage()->read()
chain. Examples below.您可以按照 Teez 建议的方式访问数据,也可以从 Zend_Session_Namespace 中提取数据。
You can access the data the way Teez suggest or just pull it from Zend_Session_Namespace.
将数组分配给会话时,必须为要创建的会话提供名称,即必须在执行 getStorage 之前执行 setStorage。
你必须这样编写你的代码:
然后要获取你的存储值,你必须使用这个:
并且你将 $y 中的所有内容作为对象获取。
享受!
assigning an array to a session, you must provide a name to the session you area creating, i.e. you must do setStorage before you do getStorage.
you must write your code like this:
and then to get your storage value, you must use this:
and you get everything in $y as an object.
Enjoy!
这是我的方法,效果很好:
bootstrap /* 中定义 init 函数
1-i 首先在登录操作中的 ,在正确的用户名和密码之后;密码*/
This is my approach and it s working nice:
1-i start by defining an init function in the bootstrap
/* in the Login action,after correct username & pwd */
user = Zend_Auth::getInstance()->getIdentity();
if(!@$this->user){
$objSession->errorMsg = " Please Login First .. ! ";
$this->_redirect('/user/login');
}
?>
user = Zend_Auth::getInstance()->getIdentity();
if(!@$this->user){
$objSession->errorMsg = " Please Login First .. ! ";
$this->_redirect('/user/login');
}
?>