CakePHP';用户登录系统
我正在尝试用CakePHP编写一个应用程序。我遇到了一个问题,它的登录系统,我需要帮助 我建立了一个名为users的数据库表,其中包含id、用户名和密码字段。密码类型为varchar,长度为50 我的User.php模型如下所示:CakePHP';用户登录系统,php,cakephp,authentication,login,system,Php,Cakephp,Authentication,Login,System,我正在尝试用CakePHP编写一个应用程序。我遇到了一个问题,它的登录系统,我需要帮助 我建立了一个名为users的数据库表,其中包含id、用户名和密码字段。密码类型为varchar,长度为50 我的User.php模型如下所示: <?php App::uses('AppModel', 'Model'); class User extends AppModel { public $validate = array( 'id' => array( 'rule
<?php
App::uses('AppModel', 'Model');
class User extends AppModel {
public $validate = array(
'id' => array(
'rule' => 'blank',
'on' => 'create'
),
'username' => array(
'alphaNumeric' => array(
'required' => true,
'rule' => 'alphaNumeric',
'message' => 'Alleen letters en cijfers zijn toegestaan'
),
'between' => array(
'rule' => array('between', 5, 20),
'message' => 'Gebruikersnaam moet tussen de 5 en 20 tekens zijn'
),
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Dit veld mag niet leeg zijn'
)
),
'password' => array(
'between' => array(
'rule' => array('between', 5, 50),
'message' => 'Wachtwoord moet tussen de 5 en 50 tekens zijn',
'required' => true
),
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Dit veld mag niet leeg zijn'
)
)
);
public function beforeSave($options = array()) {
if (isset($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}
}
?>
//login function. Logs in a user
public function login() {
//if already logged in
if ($this->Session->check('Auth.user')) {
$this->redirect(array('action' => 'index'));
}
$user = $this->User->findById(2);
debug($user);
if ($this->request->is('post')) {
debug($this->request->data['User']['password']);
debug($this->request->data);
debug(AuthComponent::password($this->request->data['User']['password']));
debug($user['User']['password']);
if ($user['User']['password'] == AuthComponent::password($this->request->data['User']['password'])) {
echo 'user pw == hashed request data pw <br />';
}
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Gebruikersnaam of wachtwoord is onjuist.'));
}
}
}
问题是,一切似乎都是正确的,但每次都会给我错误的用户名或密码。我甚至试着根本不加密密码,但也没用。我做错了什么?我如何修复它
附言:我对cakephp不是很有经验,所以如果我犯了一个明显的错误,我很抱歉。我使用了cakephp网站上的教程
编辑:
因此,我将一些调试打印更改为使用debug()方法,并且还从auth组件中删除了与passwordhasher有关的某个部分。我刚刚尝试再次登录,现在登录完全正常。我想这就是问题存在的原因
这是我的代码:
'Form'=>数组(
“passwordHasher”=>数组(
'className'=>'Simple',
“hashType”=>“sha256”
)
)
我将其更改为“表单”,现在它可以工作了。基本上,通过使用:
'authenticate' => array(
'Form' => array
'passwordHasher' => array(
'className' => 'Simple',
'hashType' => 'sha256'
)
)
),
在登录操作中执行密码检查时,您告诉AuthComponent::login()
方法使用sha256哈希类型
但是,在
User::beforeSave()
回调中,您使用了旧的AuthComponent::password()方法来散列密码并将其存储在数据库中。此方法不接受hashType参数,默认hashType为sha1。如果确实要继续使用sha256和上面的代码,请更改beforeSave()
方法以使用新的SimplePasswordHasher
,如图所示。(我假设您正在使用Cake 2.4.x)如果您共享一些调试信息,将会有所帮助。Like-调试后的哈希密码与数据库中的密码相同吗?如果没有,那就缩小了很多范围。另外-学习使用Cake的debug()
-更好,你只需要做一次,而不是用回音来解释,用pr来获取数据。我编辑了我的问题,因为我解决了这个问题。我对提供大量代码以及你自己能够解决这个问题表示赞赏。干得好!谢谢你的回答。使用sha256加密是否更好?根据我所读的,是的。好的,我将使用sha256。谢谢你的意见!:)
'authenticate' => array(
'Form' => array
'passwordHasher' => array(
'className' => 'Simple',
'hashType' => 'sha256'
)
)
),