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

{


	$user=User::model()->with('role')->find('LOWER(loginName)=?',array(strtolower($this->username)));


	if($user===null)


	{


		$this->errorCode=self::ERROR_USERNAME_INVALID;


	}


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





	{


		$this->errorCode=self::ERROR_PASSWORD_INVALID;


	}


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


		$this->errorCode=self::ERROR_STATUS_NOTACTIVE;


	else


	{


		$this->_id=$user->id;


		$this->username=$user->loginName;


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


		$this->errorCode=self::ERROR_NONE;


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


$_SERVER['REMOTE_ADDR'] ));


		$user_log= new UserLog();


		$user_log->id=$user->id;


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


		$user_log->ip=$_SERVER['REMOTE_ADDR'];


		$user_log->save();


		


	}


	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=User::model()->findByPk($user->id);

$user->lastLogin=$user->currentLogin;

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

$user->save();



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 - http://www.yiiframework.com/wiki/6/

Best solutions is here, follow this wiki -

How to get & store Last Login DateTime in database

http://www.yiiframework.com/wiki/742/how-to-get-store-last-login-datetime-in-database/