1

在我的entry控制器中,我设置:

这有效:

$authadapter = new Test_Auth_Adapter_DbTable($db);
$auth = Zend_Auth::getInstance();

$result = $auth->authenticate($authadapter);
$data = $authadapter->getResultRowObject(null, 'password');
$auth->getStorage()->write($data);
$this->_redirector->gotoUrl('/main');

这不会:

$authadapter = new Test_Auth_Adapter_DbTable($db);
$auth = Zend_Auth::getInstance();

$auth->setStorage(new Zend_Auth_Storage_Session('Test_User')); //the only difference

$result = $auth->authenticate($authadapter);
$data = $authadapter->getResultRowObject(null, 'password');
$auth->getStorage()->write($data);
$this->_redirector->gotoUrl('/main');

当我使用调试器时,我可以看到它在 $_SESSION var 中设置了所有正确的数据,但是在设置了数据并且我重定向到所需的目的地之后,$_SESSION var 不再设置,因此我无法访问东西!

被重定向到检查身份验证的页面:

$this->auth = Zend_Auth::getInstance();
        if (!$this->auth->hasIdentity()) {
            $this->_redirector->gotoUrl('/entry');
        }

为什么这不起作用?

4

1 回答 1

4

试试这个:

$this->auth = Zend_Auth::getInstance();
$this->auth->setStorage(new Zend_Auth_Storage_Session('Test_User'));
if (!$this->auth->hasIdentity()) {
    $this->_redirector->gotoUrl('/entry');
}

问题是如果您没有设置存储类,它将默认使用 Zend_Auth_Storage_Session 和 Zend_Auth 命名空间。而且因为您的会话数据不在该名称空间中,所以 Zend_Auth 看不到它并且表现得好像用户没有登录一样。

根据您的应用程序的结构(以及身份验证的一部分),您可能希望在引导方法中执行此操作,因此您只需执行一次:

protected function _initAuth()
{
    $auth = Zend_Auth::getInstance();
    $auth->setStorage(new Zend_Auth_Storage_Session('Test_User'));

    return $auth;
}
于 2010-10-04T22:07:51.343 回答