登录函数 CakePHP

Login function CakePHP

我在 PHP on the cake 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');
        }

}
}
}

that will not make it past the first if statement.

当然不会,因为你有&& $this->Auth->user()条件。由于用户未登录 $this->Auth->user() 将 return 为空并且不会进入 if 块。