获取上次登录时间

时间:2012-02-07 11:24:03

标签: php yii

在我的应用程序中,我希望当用户登录时,他/她可以看到他/她的上次登录时间,就像我们在用户模块中登录一样。所以这样做就像我刚刚跟着这个link 。所以我制作了像这样的UserIdentity代码

<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
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;
     $id=Yii::app()->user->id;
     $lastLoginTime=Yii::app()->user->lastLoginTime;
    }
}

And to show the last login time and user name I changed my view >> column2.php file like this

<?php $this->beginContent('//layouts/main'); ?>
<div class="container">
  <div class="span-19">
    <div id="content">
      <?php echo $content; ?>
    </div><!-- content -->
  </div>
  <div class="span-5 last">
    <div id="sidebar">
      <?php if(Yii::app()->user->id):?>
      <span class="admin-message">Hello,&nbsp; <span><?php echo yii::app()->user->name;?>&nbsp;</span></span>
      <?php echo Yii::app()->user->lastLoginTime;?>
    <?php endif;?>
    <?php
      $this->beginWidget('zii.widgets.CPortlet', array(
        'title'=>'Operations',
      ));
      $this->widget('zii.widgets.CMenu', array(
        'items'=>$this->menu,
        'htmlOptions'=>array('class'=>'operations'),
      ));
      $this->endWidget();
    ?>

    <?php
      if(Yii::app()->getModule('user')->isAdmin()):
        $this->beginWidget('zii.widgets.CPortlet',array(
        'title' => 'Admin Operations',
        ));
        $this->widget('zii.widgets.CMenu', array(
        'items'=>array(
          array("label"=> "Create User", "url"=>array('/user/admin/create')),
          array("label"=> "List User", "url"=> array('/user')),
          array("label"=>"Manage Profile","url"=>array('/user/profile')),
          array("label"=>"Manage Profile Fields","url"=>array('/user/profileField/admin')),
        ),
        'htmlOptions'=>array('class'=>'operations'),
      ));
      $this->endWidget(); ?>
     <? endif;
    ?>
    </div><!-- sidebar -->
  </div>
</div>
<?php $this->endContent(); ?>

它在登录后显示用户名,但是当我想检查上次登录时间时,它显示的错误如下: Property&#34; CWebUser.lastLoginTime&#34;没有定义。有人可以指导我如何做到这一点吗?

2 个答案:

答案 0 :(得分:1)

使用

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

答案 1 :(得分:0)

不知道为什么不工作。 我认为如果你不能使用会话它应该工作。

Yii::app()->session['lastLoginTime'] = $user->lastLoginTime; 
echo Yii::app()->session['lastLoginTime'];