简体   繁体   English

在Yii Framework中获取上次登录时间

[英]Getting last login time in Yii Framework

I am a newbie to Yii framework. 我是Yii框架的新手。 I had asked this question over Yii forum, but not got any good result, so I came here. 我曾在Yii论坛上问过这个问题,但没有得到很好的结果,所以我来到了这里。 Actually I want to show last logged in time when admin will login. 实际上,我想显示管理员登录时的上次登录时间。 It is available in Yii user module. 它在Yii用户模块中可用。 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: 我按了此链接,并按照说明制作了如下的Useridentity代码:

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. 现在我必须在视图文件中调用idlastlogin ,以便获得lastlogin时间。因此我在视图文件中使用了此代码。

<?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 您不能直接访问通过setState()分配的变量

If you are using setState() you have to use getState() to get it: 如果使用setState() ,则必须使用getState()来获取它:

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. 或者您可以再添加一个私有属性(例如$_id并以相同的方式使用它。 (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. 我怀疑更改尚未影响您的会话,这就是为什么CWebUser.lastLoginTime无法访问的原因。 See also the details of CWebUser.__get() . 另请参见CWebUser .__ get()的详细信息。

you need just write that in your view when the user is logged 您只需在用户登录后在视图中写上

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

try it! 试试吧!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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