登录功能CakePHP

时间:2015-10-08 14:44:49

标签: php cakephp cakephp-2.7

我在PHP框架2.7上有一个登录函数,它不会超过第一个if语句。条件似乎总是假的,我真的很困惑为什么。我没有正确实施请求吗?任何帮助将不胜感激。

登录()

function login() { 

if (!empty($this->request->data) && $this->Auth->user()) {

  // Delete all old tokens
  $this->Tour->recursive = -1;
  $this->Tour->deleteAll(array('Tour.userid' => $this->Auth->user('userid')));
  // Create a new token

  $this->Tour->create();
  $this->Tour->save(array('token' => md5(rand()), 'userid' => $this->Auth->user('userid')));
  // Update login count
  $user = $this->User->read(null, $this->Auth->user('userid'));
  $user['User']['logincount']++;
  $this->User->saveField('logincount', $user['User']['logincount']);
  // Update last login time
  $this->User->saveField('lastlogin', date('Y-m-d h:m:s'));
  echo 'doesnt work';
  if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            echo 'authLogin';
            return $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
        }

}
}
}

1 个答案:

答案 0 :(得分:3)

  

不会超过第一个if语句。

当然,由于你有&& $this->Auth->user()条件,它不会胜利。由于用户未登录$this->Auth->user()将返回null,并且无法输入if块。