2011-12-29 66 views
0

我是Yii框架的新手。我在Yii論壇上問過這個問題,但沒有得到任何好的結果,所以我來到了這裏。其實我想在管理員登錄時顯示上次登錄時間。它在Yii用戶模塊中可用。那麼該怎麼做。是否有可能從用戶模塊到索引頁面獲得這些時間?任何幫助和建議都將非常可觀。在Yii框架中獲取最後登錄時間

[更新]

我跟着this link,我犯了這樣的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; 
    } 
} 

現在我要打電話idlastlogin鑑於文件,這樣就可以得到lastlogin time.So我已經在視圖文件中使用了這段代碼。

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

所有更改後,我得到了像錯誤:

Property "CWebUser.lastLoginTime" is not defined. 

回答

3

你可以試試這個:

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; 
+0

我在視圖文件中做了它,但它顯示了像這樣的錯誤解析錯誤:語法錯誤,意外的T_VARIABLE。 – NewUser 2011-12-30 08:29:38

0

你只需要寫在你看來,當用戶登錄

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

試試吧!

0

如果您正在使用setState()你必須使用getState()得到它,你不能訪問通過setState()直接

賦值的變量:

的UserIdentity

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

視圖

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

或者您可以添加一個像$_id這樣的私人財產,並以同樣的方式使用它。 (我推薦它!)

+0

這是無稽之談。 'CWebUser'以覆蓋'__set()'方法的方式來檢查會話內容。 – DaSourcerer 2013-11-27 13:51:52

0

這可能聽起來很愚蠢,並且都是本着「你試過打開還是關閉?」的精神,但根據this comment,你是否嘗試過註銷並重新登錄?我懷疑這個變化並沒有影響你的會話,這就是爲什麼CWebUser.lastLoginTime無法訪問。另請參閱CWebUser.__get()的詳細信息。