0

I'm having a difficult time understanding how Zend_Session_Namespace is integrated with Zend_Auth. I have this method I'm using as the action to Authenticate my login page-it is working correctly and redirecting to the /monthly view:

 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('users')
                ->setIdentityColumn('UserName')
                ->setCredentialColumn('Password');    

// Set the input credential values
    $uname = $request->getParam('UserName');
    $paswd = $request->getParam('Password');
    $authAdapter->setIdentity($uname);
    $authAdapter->setCredential($paswd);

   // Perform the authentication query, saving the result
    $result = $auth->authenticate($authAdapter);

       // TRYING TO SET THE NAMESPACE
        $this->session = new Zend_Session_Namspace('UserName');

    if($result->isValid()){
        $data = $authAdapter->getResultRowObject(null,'password');
        $auth->getStorage()->write($data);
        $this->_redirect('/monthly');

    }else{
        $this->_redirect('/login');
    }
}

But I need to be able to store UserName as a Zend_session and call it from monthly controller. I'm not doing things right because I just get a blank screen when I try and do this:

public function indexAction()
{

$this->view->userName = Zend_Session_Namespace('UserName');
}
4

3 回答 3

1

您没有使用正确的命名空间。Zend_Auth 使用 Zend_Auth 命名空间。命名空间是结构,而不是值的键。所以你的会话看起来像这样:

Array('Zend_Auth' => array ('UserName' => 'myname')

嗯,这并不准确,因为您没有存储用户名,除非您直接在适配器中提供了这一点。您需要执行以下操作:

$auth->getStorage()->UserName = 'myusername';

然后您可以使用$authData = new Zend_Session_Namespace('Zend_Auth'); $username = $authData->UserName;.

仔细看看 Zend_Auth_Adapter_Db 是如何工作的。

于 2010-08-02T05:37:17.963 回答
1

随着线条:

$data = $authAdapter->getResultRowObject(null,'password');
$auth->getStorage()->write($data);

您正在编写除密码之外的所有用户信息,这没关系。无论您需要访问登录用户的详细信息,您都可以执行以下操作(根据您的评论更新):

public function indexAction() {
    $auth = Zend_Auth::getInstance();
    if($auth->hasIdentity()) {
        $userData = $auth->getIdentity();
        $this->view->user = $userData;
    }
}

在视图文件 ( index.phtml) 中:echo $this->user->firstname

而已。如果有一天您决定将 Zend_Auth 的存储从会话更改为例​​如数据库,那么这段代码仍然可以工作。

于 2010-08-02T15:43:12.833 回答
0

这是我的方法,效果很好: 1-i 首先在引导程序中定义一个 init 函数

protected function _initSession()

{

$UserSession = new Zend_Session_Namespace('UserSession');
$UserSession->setExpirationSeconds(/* you may fix a limit */);
Zend_Registry::set('UserSession', $UserSession);

}

/* 在登录操作中,在正确的用户名和密码之后 */

// Create session
$UserSession = Zend_Registry::get('UserSession'); 
// Get the user from database or just from fields
//you have to make sure that the psswd is encrypted use MD5 for example..
 $db = Zend_Db_Table::getDefaultAdapter();
$user = $db->fetchRow("SELECT * FROM user_table WHERE user_email = '".$user_email."'");
//then you assign to $user to $UserSession variable : 
$UserSession->user = $user;
//finaly don't forget to unset session variable in the Logout action ...
于 2013-06-05T14:35:13.360 回答