2015-10-09 78 views
0

我目前使用Auth組件登錄。我想在用戶登錄我的網站時更新用戶表中的last_login字段。如何使用cakephp 2.x中的authcomp更新上次登錄

我在控制器的用戶登錄功能我have--

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' 
    ); 
    } 
+0

你是什麼意思 「更新LAST_LOGIN場」?它是上次登錄user_id或日期時間的字段嗎? –

回答

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 
} 
相關問題