简体   繁体   中英

Getting Last login time

In my application I want that when a user will login he/she can see his/her last login time just like when we do login in user module.So for doing like that that I just followed this link . So I made my UserIdentity code like this

<?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(); ?>

It is showing the username after login but when I want to check the last login time it is showing error like: Property "CWebUser.lastLoginTime" is not defined. Can some one guide me how to do this?

采用

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

Don't know why not working. I think it should work if it doesn't you can use session.

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

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