2012-02-07 118 views
0

在我的應用程序中,我希望當用戶登錄時,他/她可以看到他/她的最後一次登錄時間,就像我們在用戶模塊中登錄一樣。所以爲了這樣做,我只是遵循這個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(); ?> 

它顯示登錄後的用戶名,但是當我要檢查上次登錄時間它顯示像錯誤: 屬性「CWebUser.lastLoginTime」沒有定義。有人能指導我如何做到這一點?

回答

1

使用

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

雅我已經用它..它根本不工作。 – Jagdish 2012-02-08 19:10:12

0

不知道爲什麼不工作。 我認爲它應該工作,如果它不能使用會話。

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