【问题标题】:Yii user authentication and session not workingYii 用户身份验证和会话不起作用
【发布时间】:2012-05-19 05:17:21
【问题描述】:

我正在 Yii 中建立一个网站,但我卡在了用户身份验证步骤。无论我做什么,我都无法验证用户身份或在会话中存储一些值。

我的 UserIdentity 组件如下:

class UserIdentity extends CUserIdentity
{
    private $_id;

    public function authenticate(){
        if(($userRecord = Admin::model()->findByAttributes(array('username'=>$this->username)))===null){
            return $this->errorCode=self::ERROR_USERNAME_INVALID;
        }
        if($userRecord->password!=hash('sha256', $this->password)){
            return $this->errorCode=self::ERROR_PASSWORD_INVALID;
        }
        $this->_id=$userRecord->id;
        $this->setState('title', $userRecord->username);
        Yii::app()->session['clientId'] = $userRecord->clientId;
        Yii::app()->session['id'] = $userRecord->id;
        Yii::app()->session['loggedin'] = true;

        return $this->errorCode=self::ERROR_NONE;
    }

    public function getId(){
        return $this->_id;
    }

}

我的 Yii:app()->user->isGuest 总是返回 true 而 Yii::app()->user->id 什么也不返回。

这就是我尝试使用Yii::app()->session['loggedin'] 进行身份验证的原因。但即使这样也行不通。当我像Yii::app()->session 这样访问会话数组时,我收到错误Property "CWebUser.session" is not defined.

这真是令人沮丧。我是 Yii 的新手。请帮忙。

【问题讨论】:

  • login用户了吗?
  • 哦,嘘。我在测试时已经从模型中注释掉了登录功能。谢谢你。现在可以完美运行了。
  • 太好了,我已将其添加为答案。

标签: php yii yii-components


【解决方案1】:

使用CWebUser的login函数。

关注this tutorial

【讨论】:

    猜你喜欢
    • 2014-05-09
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2019-07-15
    • 2015-07-13
    • 1970-01-01
    相关资源
    最近更新 更多