Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP Save Date/Time on Login

I have the bellow login function in my controller and am trying to save the login date/time into the users database table under the field last_login that is a DATETIME field, login works fine but the field never gets populated.. any ideas on what may be stopping this or how to troubleshoot what should be getting output?

public function login() {
    $this->layout = 'login';
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {

            $this->User->id = $this->Auth->user('id'); // target correct record
            $this->User->saveField('last_login', date(DATE_ATOM)); // save login time

            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash('Invalid username or password, try again', 'default', array('class' => 'warning'));
        }
    }
like image 435
medoix Avatar asked Dec 03 '25 17:12

medoix


1 Answers

If your last_login field in db is set to DATETIME then, you need to do:

$this->User->saveField('last_login', date("Y-m-d H:i:s"));

As DATE_ATOM returns something like 2012-08-15T15:52:01+00:00, so it wont get inserted Try disabling AuthComponent::autoRedirect, like

public $components = array(
    'Auth' => array(
        ......
        'autoRedirect' => false  //set to false to disable auto redirect
    ),
);
like image 167
Sudhir Bastakoti Avatar answered Dec 06 '25 09:12

Sudhir Bastakoti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!