insert last login time

Hello Guys,

In User table, I have one field, name lastLogin time. I can insert login time, but that is current login time. I want to display user’s last login time. Do I need two fields, lastLogin and currentLogin. If yes then how to update currentLogin field to lastLogin and insert new login time to currentLogin field when user login. I think I confuse U. Actually I want to display user’s last login time.

Here is my component/UserIdentity.php file

public function authenticate()







	else if(md5($this->password)!==$user->password)




	else if($user->active == 0)






		$this->_role=$user->role->name;  // save role for use in app


		User::model()->updateByPk($user->id,array('lastLogin'=> date("Y-m-d H:i:s"), 'ip'=> $_SERVER['REMOTE_ADDR'] ));


		$user_log= new UserLog();


		$user_log->time=date("Y-m-d H:i:s");





	return !$this->errorCode;


Hi Nemo,

Have you figured out how to store the login time yet? Please let me know your implementation - thanks!

you could do it with two fields or using session. When the user logs in copy the data from last_login field to session and than if session is not empty insert the new time() into last_login. The second suggestion would allow you to keep only the very last login info until the session is alive

what about:



$user->currentLogin=date("Y-m-d H:i:s");


i would go for a session-variant…

so before you update the last_login time write the data to your session… and update the last_login field…

now you always can display the last_login time on your page using the sessiondata…

edit some code:

Yii::app()->user->last_login = $usermodel->last_login; // afaik yii::app()->user writes to a cookie or session

$usermodel->last_login = time();

echo "welcome back - your last login was at:".Yii::app()->user->last_login;

Hi, there! I think this is the best way to do this -

Best solutions is here, follow this wiki -

How to get & store Last Login DateTime in database