nemo
(Nemoneel)
April 29, 2010, 5:39pm
1
Hey,
How can I track, when user was logged in for the last time using Yii? When I used simply PHP, I redefined session storage to DB and was tracking session end, and put this time to the "lastLogged" field in the user table. How can I achieve this with Yii?
Vince
(Vadimg88)
April 29, 2010, 6:36pm
2
Probably the same way, Make a column in the users table that will store the last login time and update it each time a member logs in.
nemo
(Nemoneel)
April 29, 2010, 7:03pm
3
Is there any other solution? If not, then how to transfer newLogin dateTime to oldLogin time
here is my code
UserIdentity.php
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 if($user->locked == 0)
$this->errorCode=self::ERROR_STATUS_LOCKED;
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('newLogin'=> date("Y-m-d H:i:s"), 'ip'=> $_SERVER['REMOTE_ADDR'] ));
User::model()->updateByPk($user->id,array('oldLogin'=> date("Y-m-d H:i:s")));
$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;
}