Yii::app()->user->isGuest always returns true even

2019-04-21 05:33发布

I started to make some differences between those users which have authenticated and those that not. For this, i am using

Yii::app()->user->id;

However, in a determined view i put the following code:

<?php 
    if(Yii::app()->user->isGuest) {
        print("Welcome back Guest!");
        print("Your id is ".Yii::app()->user->id);
    } else {
        print("Welcome back ".Yii::app()->user->name);
        print("Your id is ".Yii::app()->user->id);
}?>

And i always get the "welcome back guest!", whether i have logged in (successfully) or not. And if i have logged in, then it displays the welcome message together with the user's id!

EDIT

@briiC.lv Hey.. sorry for the late reply, I hope you are still following this! I am not extending the given UserIdentity class. Is this mandatory? Since i still dont get very well the whole authorization issue, i thought it would be best to give a try with the class they provide, and then extend with my own functionality.. Anyway, next i post my UserIdentity class with its small tweaks.. maybe the problem lies here??

<?php class UserIdentity extends CUserIdentity{
private $_id;

public function authenticate()
{   
    $user = Users::model()->findAll('username=\''.$this->username.'\' AND password=\''.$this->encryptedPassword.'\'');
    if(!isset($user[0]))
    {
        return false;
    }
    else 
    {   
        $this->setState('id', $user[0]->id);            
        $this->username = $user[0]->username;
        $this->errorCode=self::ERROR_NONE;
        return true;
    }
}

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

}

Here is the output i got when i started to log as you suggested; i got this output immediately after successfully logging in.

[05:23:21.833][trace][vardump] CWebUser#1 ( 
[allowAutoLogin] => true 
[guestName] => 'Guest' 
[loginUrl] => array ( '0' => '/site/login' ) 
[identityCookie] => null 
[authTimeout] => null 
[autoRenewCookie] => false 
[autoUpdateFlash] => true 
[CWebUser:_keyPrefix] => '0f4431ceed8f17883650835e575b504b' 
[CWebUser:_access] => array() 
[behaviors] => array() 
[CApplicationComponent:_initialized] => true 
[CComponent:_e] => null 
[CComponent:_m] => null 
)

Any help is much appreciated!

8条回答
爷、活的狠高调
2楼-- · 2019-04-21 06:30

check your security configuration for cookies and sessions. disable session.use_only_cookies & session.cookie_httponly in php.ini file.

查看更多
祖国的老花朵
3楼-- · 2019-04-21 06:31

The error is in the following line

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

As seen in the official yii documentation regarding auth & auth, setState should be used for anything but the id field. In order to implement the key Yii will use to identify your user, return a unique value per user in the Identity getId() function.

In your case, this means you simply have to change the above line into the following:

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

Regarding the actual inner working of the login procedure, I'd recommend a look at the CWebUser class, and especially at its login function, which is responsible for the actual storage of the Identity getId() return value.

查看更多
登录 后发表回答