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 変数に設定されていることがわかりますが、データが設定され、目的の宛先にリダイレクトされた後、$_SESSION 変数が設定されていないため、アクセスできません!

にリダイレクトされるページは、次の方法で認証をチェックします。

$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 名前空間で Zend_Auth_Storage_Session を使用することです。セッション データはその名前空間にないため、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 に答える