我是CakePHP的新手,我使用的是2.8.5版本。我已按照教程添加身份验证到我的网站:http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html,但我在尝试登录时遇到问题。
我可以添加新用户,但每当我尝试登录时,都会返回false并获得:
用户名或密码无效,请重试。
我也得到:
警告(512):无效盐:针对河豚的pass01请访问http://www.php.net/crypt并阅读有关建造河豚盐的相应部分。 [CORE / Cake / Utility / Security.php,第323行]
(pass01是密码)。我已经按照本教程的说明完全关注了河豚哈希,但我的数据库中的密码似乎没有哈希(它们只是按原样显示)。
user.php的
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
class User extends AppModel {
public $validate = array(
'username' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'A username is required'
)
),
'password' => array(
'required' => array(
'rule' => 'notBlank',
'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'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
return true;
}
}
来自 UsersController.php
的登录功能public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}
login.ctp
<div class="users form">
<?php echo $this->Flash->render('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend>
<?php echo __('Please enter your username and password'); ?>
</legend>
<?php echo $this->Form->input('username');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
我已经查看了所有类似的问题,但似乎没有一个解决方案对我有用。我真的很感激任何帮助。