如何在Yii中更新用户登录时的TIMESTAMP?

时间:2012-06-16 06:43:05

标签: php login yii

每当用户登录我的网站(使用Yii框架构建)时,我希望能够使用TIMESTAMP更新名为last_login_at的用户模型中的字段。

如何做到这一点?

我想SiteController.php中的actionLogin()需要进行某种类型的编辑。

3 个答案:

答案 0 :(得分:6)

您要覆盖的是CWebUser::afterLogin方法,它应该是这样的:

protected function afterLogin($fromCookie) {
  if (!$fromCookie) { #User Explicitly logged in
    $user = $this->model;
    $user->saveAttributes(array('last_login_at' => date(DateTime::W3C)));
  }
  return parent::afterLogin($fromCookie);
}

答案 1 :(得分:2)

调用UserIdentity :: authenticate()时会发生身份验证。

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 !== $user->encrypt($this->password)) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        } else {
            $this->_id = $user->id;
            if (null === $user->last_login_at) {
                $lastLogin = time();
            } else {
                $lastLogin = strtotime($user->last_login_at);
            }
            $this->setState('lastLoginAt', $lastLogin);
            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    public function getId()
    {
        return $this->_id;
    }

}

现在lastLoginAt在您的会话中。接下来的问题是“我怎样才能获得这个价值?”。这就是答案。这是我用来在

中显示lastLoginAt的代码
'lastLoginAt' => Yii::app()->user->isGuest ?
    null :
    date('l, F d, Y, g:i a', Yii::app()->user->lastLoginAt)

Linke:

public function actionIndex()
{
    // renders the view file 'protected/views/site/index.php'
    // using the default layout 'protected/views/layouts/main.php'
    $this->render('index', array(
        'lastLoginAt' => Yii::app()->user->isGuest ?
                null :
                date('l, F d, Y, g:i a', Yii::app()->user->lastLoginAt)
    ));
}

此外,您希望将此值保存在数据库中。因此,尝试使用:

更改LoginForm类
User::model()->updateByPk($this->_identity->id, array(
    'last_login_at' => new CDbExpression('NOW()')
));

以这种方式使用此代码段:

class LoginForm extends CFormModel
{
    public function login()
    {
        if ($this->_identity === null) {
            $this->_identity = new UserIdentity($this->username, $this->password);
            $this->_identity->authenticate();
        }
        if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
            $duration = $this->rememberMe ? 3600 * 24 * 30 : 0; // 30 days
            Yii::app()->user->login($this->_identity, $duration);
            User::model()->updateByPk($this->_identity->id, array(
                'last_login_at' => new CDbExpression('NOW()')
            ));
            return true;
        } else {
            return false;
        }
    }
}

答案 2 :(得分:2)

您自己在问题中给出了答案,请参阅最后一行 在您的站点控制器登录操作

public function actionLogin()
 {
    $model=new LoginForm;

    // if it is ajax validation request
    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }

    // collect user input data
    if(isset($_POST['LoginForm']))
    {
        $model->attributes=$_POST['LoginForm'];
        // validate user input and redirect to the previous page if valid
        if($model->validate() && $model->login())

    $userid=Yii::app()->user->id;//to get user id 
           $timestamp=date('Y-m-d H:i:s');//current time stamp
     User::model()->updateByPk($userid, array('last_login_time' =>$timestamp));//hope last_login_time field in user table



            $this->redirect(Yii::app()->user->returnUrl);
            //$this->redirect(array('loginsuccess'));
    }
    // display the login form
    $this->render('login',array('model'=>$model));
  }
相关问题