Record last login time

Hi,

I need to record the time that a user is log in. I extend the blog tutorial, so I have the LoginForm.php (extend CFormModel) model. But, I am not sure how can I store the time to the user model (User.php) from the LoginForm.php.

Anyone can help?

Thank you,

Daniel

Hi Daniel

In the Site controller’s Login function, after you have loaded the user’s model, you should add a line to update the user’s record. Eg.


$user->saveAttributes(array('lastLogin'=>'2009-09-30'));

Hi Alex,

Thank you for the quick response. But, I am still not sure how to do this. I do not have a Login function on my SiteController. The login is handled by Portlet, UserLogin.php.




class UserLogin extends Portlet

{

	public $title='Login';


	protected function renderContent()

	{

		$form=new LoginForm;

		if(isset($_POST['LoginForm']))

		{

			$form->attributes=$_POST['LoginForm'];

			if($form->validate())

				$this->controller->refresh();

		}

		$this->render('userLogin',array('form'=>$form));

	}

}



While the LoginForm model do not have direct access to User model since its purpose is only for login. Should I add the save attribute in LoginForm model when the authentication is success?




public function authenticate($attribute,$params)

{

	if(!$this->hasErrors())  // we only want to authenticate when no input errors

	{

		$identity = new UserIdentity( $this->username, $this->password );

		$identity->authenticate();

		switch( $identity->errorCode )

		{

			case UserIdentity::ERROR_NONE:

				Yii::app()->user->login($identity);

[b]------------------------------->?? but how could I get the user object?[/b]

				break;

			case UserIdentity::ERROR_USERNAME_INVALID:

				$this->addError('username','Username is incorrect.');

				break;

			case UserIdentity::ERROR_USER_ACCOUNT_DISABLED:

				$this->addError('username','User Account is disabled.');

				break;

			default: // UserIdentity::ERROR_PASSWORD_INVALID

				$this->addError('password','Password is incorrect.');

				break;

		}

	}

}



or in the UserIdentity.php?




public function authenticate()

{

	$user = User::model()->findByPK( $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->Enable == User::STATUS_DISABLED )

	{

		$this->errorCode = self::ERROR_USER_ACCOUNT_DISABLED;

	}

	else

	{

		$this->_username = $user->Username;

		$this->setState( 'roleFk', $user->RoleFK );

[b]--------------->$user->saveAttributes(array('LastLogin'=>new CDbExpression('NOW()')));[/b]

		$this->errorCode = self::ERROR_NONE;

	}

	

	return !$this->errorCode;

}



The second one is give me the correct result (in UserIdentity.php), I did not try the first one. But, the timestamp is not correct. My Windows time is 7:17AM but in the LastLogin it is already 8:17AM ;( Even though, I already set the correct time zone in php.ini.

Thank you,

Daniel