Yii认证错误:登录成功后Yii::app()->user->isGuest()总是返回true


Yii Authentication error: After login successfull Yii::app()->user->isGuest() always return true

我开始在经过身份验证的用户和来宾用户之间创建一个差异。但总是当我识别用户并成功登录时,当我使用Yii::app()->user->isGuest时,它仍然返回true。下面是我使用的代码:

if (Yii::app()->user->isGuest){
            echo 'Welcome back Guest';
    } else {
            echo 'Welcome back '.Yii::app()->user->name;
    }

无论我是否(成功)登录,我总是得到"欢迎回来的客人"。如果我再次登录,就会收到这个消息。这是用户识别码。

class UserIdentity extends CUserIdentity {
    /**
     * Authenticates a user.
     */
    private $_id;
    public function authenticate() {
        $user = Users::model()->findByAttributes(array('username' => $this->username));
        if ($user === null)
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        else if ($user->password !== $this->password)
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        else {
            $this->_id = $user->id;
            $this->setState('title', $this->username);
            $this->setState('id', $user->id);   
            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
    }
    public function getId() {
        return $this->_id;
    }
} 

请帮我解决这个问题。

你应该修复LoginForm

/**
 * Authenticates the password.
 * This is the 'authenticate' validator as declared in rules().
 */
public function authenticate($attribute, $params)
{
    // we only want to authenticate when no input errors
    if (! $this->hasErrors()) {
        $identity = new UserIdentity($this->email, $this->password);
        $identity->authenticate();
        switch ($identity->errorCode) {
            case UserIdentity::ERROR_NONE:
                $duration = ($this->rememberMe)
                    ? 3600*24*14 // 14 days
                    : 0; // login till the user closes the browser
                Yii::app()->user->login($identity, $duration);
                break;
            default:
                // UserIdentity::ERROR_USERNAME_INVALID
                // UserIdentity::ERROR_PASSWORD_INVALID
                // UserIdentity::ERROR_MEMBER_NOT_APPOVED
                $this->addError('', Yii::t('auth',
                    'Incorrect username/password combination.'));
                break;
        }
    }
}

class UsersController extends Controller 
{
    /**
     * Displays the login page
     */
    public function actionLogin() 
    {
        $model = new LoginForm;
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            $valid = $model->validate();
            if ($valid) {
                $this->redirect(Yii::app()->user->returnUrl);
            }
        }
        $this->render('login', array('model' => $model));
    }
}

你能显示你的配置吗?

答案可以在这里找到yii认证错误

您也可以在这里阅读更多关于yii身份验证的信息

尝试使用Not(!)操作符

if (!Yii::app()->user->isGuest){
            echo 'Welcome back Guest';
    } else {
            echo 'Welcome back '.Yii::app()->user->name;
    }

你应该这样做:

$this->setState('id', $user[0]->id);  

setState不应该用于'id'。为了返回'id',您已经覆盖了getId()方法。

如果你确实需要设置它,像这样修改它:

$this->_id = $user[0]->id;

希望有帮助。

认为,