我是CakePHP的新手。我試着用CakePHP 2.4.1來嘗試the Cake cookbook tutorial of simple authentication。
雖然我輸入了正確的用戶名和密碼,但我總是收到「用戶名或密碼無效,請重試」。CakePHP簡單認證教程不起作用
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
根據the AuthComponent API,如果參數到Auth->login()
是空的或沒有指定,則該請求將被用來識別用戶。 調試查詢輸出顯示
SELECT `User`.`id`, `User`.`username`, `User`.`password`, `User`.`role`, `User`.`created`, `User`.`modified`
FROM `cake_dvdcatalog`.`users` AS `User` WHERE `User`.`username` = 'admin' LIMIT 1
這裏是我的用戶模型:
// app/Model/User.php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
)
);
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
您可以發佈您的用戶模型代碼嗎? – Andrew
@安德魯,當然。我編輯了我的問題。 – Sithu
嘗試調試$ this-> Auth-> login()方法。加密的密碼應該與數據庫中的相同。 – Dezigo