我有一个像这样的LoginController:
public function loginAction(){
$db = $this->_getParam('db');
$this->_helper->viewRenderer->setNoRender();
$this->_helper->getHelper('layout')->disableLayout();
$adapter = new Zend_Auth_Adapter_DbTable(
$db,
'user',
'username',
'password',
'MD5(CONCAT(?,password_salt))'
);
$adapter->setIdentity($this->_request->getParam('username'));
$adapter->setCredential($this->_request->getParam('password'));
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);
if ($result->isValid()) {
// get all info about this user from the login table ommit only the password, we don't need that
$userInfo = $adapter->getResultRowObject(null, 'password');
$users = new Application_Model_DbTable_Users();
$users->updateLastlogin($userInfo->email);
$auth->setStorage(new Zend_Auth_Storage_Session('testzf'));
$authStorage = $auth->getStorage();
$authStorage->write($userInfo);
$data = array('login'=>'success');
}
和一个ProfileController:
public function getprofileAction(){
$this->_helper->viewRenderer->setNoRender();
$this->_helper->getHelper('layout')->disableLayout();
if(Zend_Auth::getInstance()->hasIdentity()) {
$username=$this->_request->getParam('username');
$db_users = new Application_Model_DbTable_Users();
$user = $db_users->getUser($username);
}
我为Login和getprofile操作制作了AjaxCalls。我可以登录但是getprofile不起作用,因为Zend_Auth :: getInstance() - > hasIdentity()返回null。 我在application.ini中看到了文件夹中的2个会话文件。 resources.session.save_path = APPLICATION_PATH“/../data/sessions” 第一个是会话数据,第二个是空的0KB。 这应该通过Ajax-Calls工作还是我犯了错误?
此致
答案 0 :(得分:1)
由于您在登录操作(testzf
)中使用了自定义身份验证存储密钥,因此每当您想要访问身份验证数据时都需要设置此密钥:
public function getprofileAction(){
$this->_helper->viewRenderer->setNoRender();
$this->_helper->getHelper('layout')->disableLayout();
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session('testzf'));
if($auth->hasIdentity()) {
$username=$this->_request->getParam('username');
$db_users = new Application_Model_DbTable_Users();
$user = $db_users->getUser($username);
}
}
答案 1 :(得分:0)
请使用Zend_Auth_Storage_Session
在您的登录操作中
//检查ajax请求
$result = array(
'error' => true,
'msg' => 'Something went wrong,please try again!'
);
if(!$this->_request->isXmlHttpRequest()){
$this->_helper->json->sendJson($result);
}
/**
* user authentication
*/
$authAdapter = new Zend_Auth_Adapter_DbTable(null,$this->_table_user);
$authAdapter->setIdentityColumn('user_name')
->setCredentialColumn('password');
$authAdapter->setIdentity($user_name)
->setCredential($password);
$select = $authAdapter->getDbSelect();
$select->where('status = ?',1);
$result = $authAdapter->authenticate($authAdapter);
/**
* user validate
*/
if($result->isValid()){
$userStorage = new Zend_Auth_Storage_Session('Zend_Auth_User','User_Storage');
$userData = $authAdapter->getResultRowObject();
$userStorage->write($userData );
$this->_helper->json->sendJson(array('error'=>false));
}
//在您的个人资料操作中
/ ** *您将通过Zend_Auth_Storage_Session获取用户会话数据 *您可以在动作控制器中编写此功能,您可以轻松地进入控制器 比如$ this-> _helper-> json-> sendJson(array('error'=> false)); * /
$userStorage = new Zend_Auth_Storage_Session('Zend_Auth_User','User_Storage');
$userData = $userStorage->read();
假设您需要检查hasIdentity() 你应该使用默认的Zend_Auth_Storage_Session $ userStorage = new Zend_Auth_Storage_Session(); // auth存储的默认命名空间(没有任何命名空间)
您在Zend_Auth_Storage_Session中使用了任何名称空间,如Zend_Auth_Storage_Session('testzf'),您需要在$ auth实例中设置存储,如
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session('testzf'));
$auth->hasIdentity(); // you will get storage here