0

我目前正在使用 Auth 组件登录。每当用户登录我的网站时,我想更新我的用户表中的 last_login 字段。

我在用户控制器中的登录功能我有 -

public function login() {
 $this->layout = 'main';
 if ($this->request->is('post')) {
 if($this->Auth->login()) {
   $this->redirect(array('controller'=>'pages','action'=>'dashboard'));  // after login , redirect on dahsboard
  }
  $this->Session->setFlash(__('Your username or password was incorrect.'));
  }
   $this->redirect(Router::url('/', true));  // there is no login.ctp file so it always redirect on home
}

在应用程序控制器中我有

class AppController extends Controller {

public $components = array(
    'Auth',
    'Session',
);

function beforeFilter() {
    $this->Auth->loginAction = array(
        'controller' => 'users',
        'action' => 'login'
    );
    $this->Auth->logoutRedirect = array( 
       'controller' => 'pages',
      'action' => 'display','home'
     );
  }
4

1 回答 1

0

我建议您在成功登录后执行一个简单的更新查询,方法是添加这个

$user = $this->Session->read("Auth.User");
$this->User->id = $user['id'];
$this->User->saveField('last_login', date('Y-m-d H:i:s'));

还有其他几种方法可以更新 last_login 字段:1。

$data['User']['last_login']=date('Y-m-d H:i:s');
$this->User->save($data);

2.

$this->User->updateAll(array('User.last_login'=>date('Y-m-d H:i:s')),array('User.id'=>$user['id']));

在此之后,您的代码将如下所示,

public function login() {
 $this->layout = 'main';
 if ($this->request->is('post')) {
 if($this->Auth->login()) {
       $user = $this->Session->read("Auth.User");
       $this->User->id = $user['id'];
       $this->User->saveField('last_login', date('Y-m-d H:i:s'));

   $this->redirect(array('controller'=>'pages','action'=>'dashboard'));  // after login , redirect on dahsboard
  }
  $this->Session->setFlash(__('Your username or password was incorrect.'));
  }
   $this->redirect(Router::url('/', true));  
// there is no login.ctp file so it always redirect on home
}
于 2015-10-09T05:43:31.013 回答