Yii :: application () & # 8594; user-> identifier; returns username instead of id

I have a very strange problem in that the Yii::app()->user->id;username is returned on one machine , but on the other computer that runs the identical code, I get the identification number as expected. How Yii::app()->user->idto get username? What did I miss?

+5
source share
7 answers

first, let's see what happened after logging in.

after

    $identity->authenticate();

if

    $identity->errorCode===UserIdentity::ERROR_NONE

then we will send the login action

    Yii::app()->user->login($identity,$duration)

and what kind of login?

I am scanning the yii source the main idea is

    $this->changeIdentity($id,$identity->getName(),$states);

in the entry function to the CWebUser class.

below is the changeIdentity function

    protected function changeIdentity($id,$name,$states)
{ 
    Yii::app()->getSession()->regenerateID(true);
    $this->setId($id);
    $this->setName($name);
    $this->loadIdentityStates($states);
}

secondly: stop about the problem

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

, getId() , CWebUser, ( )/protected/config/main.php :

    'components'=>array(
    'user'=>array(
        'class'=>'WebUser',
        // enable cookie-based authentication
        'allowAutoLogin'=>true,
    ),

CWebUser - WebUser.

WebUser getId(), CWebUser, WebUser CWebUser. getId() CWebUser.

https://github.com/yiisoft/yii/blob/1.1.13/framework/web/auth/CWebUser.php#LC287

     public function getId()
{
    return $this->getState('__id');
}

, "__id" ? , :

    $this->setId($id);

$id? CWebUser:

    public function login($identity,$duration=0)
{ 
    $id=$identity->getId();


    $states=$identity->getPersistentStates();



    if($this->beforeLogin($id,$states,false))
    {
        $this->changeIdentity($id,$identity->getName(),$states);

        if($duration>0)
        {
            if($this->allowAutoLogin)
                $this->saveToCookie($duration);
            else
                throw new CException(Yii::t('yii','{class}.allowAutoLogin must be set true in order to use cookie-based authentication.',
                    array('{class}'=>get_class($this))));
        }

        $this->afterLogin(false);
    }
    return !$this->getIsGuest();
}

    $id = $identity->getId();

getId $idenity, , getId UserIdentity, CUserIdentity, :

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

public function setId($id)
{
    $this->_id = $id;
    return;
}

user_id setId ($ id) UserIdentity, CUserIdentity, :    ()   {

    $record=User::model()->findByAttributes(array('user_name'=>$this->username));

    if($record===null)
        $this->errorCode=self::ERROR_USERNAME_INVALID;
    else if($record->password!==md5($this->password))
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else
    {
        $this->setId($record->user_id);


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

getId() UserIdentity.

:

private $_id;

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

, , id,

$this->_id = $user->id

, , : http://www.yiiframework.com/doc/guide/1.1/en/topics.auth#defining-identity-class

+3

, :

$this->_id=$user->id;
$this->username=$user->username;
+1

:

1) Yii::app()->user->setState("id",$user->id);
2) Yii::app()->user->id = $user->id;

$_SESSION.

, :

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

Yii::app()->user->getId()

0

, , . Useridentity. - (- > ).

    {
        $this->_id = $user->id; /*here u might miss pointer -> */
        // print_r($this->_id);exit;
        $this->errorCode = self::ERROR_NONE;
    }

, hurshid.

0

yii ( - admin demo). script sql , id id .

-1

All Articles