我不知道为什么在我拥有所有代码和数据库时仍然会收到无效的用户名或密码。此外,用户已注册但无法登录。
AppController.php
class AppController extends Controller{
public $components = array(
/*'DebugKit.Toolbar',*/
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'jobs', 'action' => 'index'),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login'
)
)
);
}
型号:User.php
App::uses('AppModel', 'Model');
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
class User extends AppModel {
public $validate = array(
'Username' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
),
'The username already exists'=>array(
'rule'=>array('isUnique'),
'message'=>'The username already exists!'
),
),
'Password' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
),
'Match passwords'=>array(
'rule'=>'matchPasswords',
'message'=>'The password does not match'
),
),
'Confirm_Password' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
),
),
);
function matchPasswords($data){
if ($data['Password'] == $this->data['User']['Confirm_Password']) {
return TRUE;
}
$this->invalidate('Confirm_Password', 'The password does not match');
return FALSE;
}
public function beforeSave($options = array()) {
if (!parent::beforeSave($options)) {
return false;
}
if (isset($this->data[$this->alias]['Password'])) {
$hasher = new SimplePasswordHasher();
$this->data[$this->alias]['Password'] = $hasher->hash($this->data[$this->alias]['Password']);
}
return true;
}
}
Controller:UsersController.php
class UsersController extends AppController {
public $components = array('Session');
var $name = 'Users';
var $helpers = array('Form');
// Placeholder for login_form, required by CakePHP to see the login_form view
function login_form() { }
public function beforeFilter () {
parent::beforeFilter();
$this->Auth->allow('user', 'login');
}
public function login(){
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Incorrect username or password'));
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
public function done() {
$this->set('framework', 'CakePHP');
}
public function user(){
if($this->request->is('post')) {
$this->User->create();
if($this->User->save($this->request->data)) {
$this->Session->setFlash('The information has been sent successfully!');
return $this->redirect(array('action' => 'login'));
$this->redirect('done');
}
}
}
}
查看:login.ctp
<h4>LOGIN HERE</h4>
<?php
echo $this->Session->flash('auth');
echo $this->Form->create('User');
echo $this->Form->input('Username');
echo $this->Form->input('Password',array('type' => 'password'));
echo $this->Form->end('Login');
?>
答案 0 :(得分:1)
您没有使用CakePHP期望的默认字段名称。因此,您必须将它们包含在<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
配置中。
尝试以下方法:
AuthComponent
这些应与class AppController extends Controller
{
public $components = array(
/*'DebugKit.Toolbar',*/
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'jobs', 'action' => 'index'),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'Username',
'password'=>'Password'
),
),
)
)
);
}
表格和视图中的字段名称相匹配。
修改强>
此外,请确保数据库中的密码字段足以存储散列密码。 CakePHP使用的默认算法是SHA1,它产生160位(40个字符)的哈希值。 users
应该足以存储此字符串。
请参阅: