简体   繁体   中英

Getting last login time in Yii Framework

I am a newbie to Yii framework. I had asked this question over Yii forum, but not got any good result, so I came here. Actually I want to show last logged in time when admin will login. It is available in Yii user module. So how to do that. Is it possible to get that time from user module to the index page.Any help and suggestions will be highly appreciable.

[Update]

I followed this link and I made Useridentity code like this as per instruction:

class UserIdentity extends CUserIdentity
{
    private $_id;

    public function authenticate()
    {
        $user=User::model()->findByAttributes(array('username'=>$this->username));
        if($user===null)
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        else if($user->password!==md5($this->password))
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        else
        {
            $this->_id=$user->id;
            $this->setState('lastLoginTime', $user->lastLoginTime);
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

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

Now I have to call id and lastlogin in view file so that I can get the lastlogin time.So I have used this code in view file.

<?php echo Yii::app()->user->name;?>
<?php echo Yii::app()->user->lastLoginTime;?>

After all the changes I got the error like:

Property "CWebUser.lastLoginTime" is not defined. 

You could try this:

Yii::app()->user->last_login = $usermodel->last_login;
$usermodel->last_login = time();

echo "welcome back - your last login was at:".Yii::app()->user->last_login;

You can not access to variables assigned via setState() directly

If you are using setState() you have to use getState() to get it:

UserIdentity

$this->setState('lastLoginTime', $user->lastLoginTime);

view

Yii::app()->user->getState('lastLoginTime');

or you can add one more private property like $_id and use it the same way. (i recommend it!)

This might sound silly and be all in the spirit of "have you tried turning it on and off again?", but as per this comment , have you tried logging out and in again? I suspect the change hasn't affected your session yet which is why CWebUser.lastLoginTime cannot be accessed yet. See also the details of CWebUser.__get() .

you need just write that in your view when the user is logged

echo Yii::app()->user->lastLoginTime;

try it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM