我无法使用$ this->使用Auth-> login()函数登录...我遵循一些教程并做了所有事情,但是当我提交表单时,它显示密码和用户名错误..虽然我检查加密的密码并匹配数据库密码...
任何想法? ... 提前谢谢
<?php
App::uses('AppController', 'Controller');
class UsersController extends AppController {
public $components = array('Paginator');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add');
$this->Auth->deny('index');
}
public function login() {
if($this->Session->read('Auth.User')){
$this->redirect($this->Auth->redirect());
}
if ($this->request->is('post')) {
print_r($this->request->data['User']['password']);
$encPassword = AuthComponent::password($this->request->data['User']['password']);
$this->request->data['User']['password'] = $encPassword;
print_r($this->request->data['User']['password'].' - '.$this->request->data['User']['username']);
//print_r($_SESSION);
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
return $this->redirect(array('controller' => 'pages','action' => 'home'));
}
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->Paginator->paginate());
}
public function view($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
}
public function edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
}
public function delete($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
$this->request->allowMethod('post', 'delete');
if ($this->User->delete()) {
$this->Session->setFlash(__('The user has been deleted.'));
} else {
$this->Session->setFlash(__('The user could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
}
?>
<?php
App::uses('AppModel', 'Model');
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'
)
)
);
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;
}
}
?>
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $helpers = array('Seo');
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home')
),
'Do',
'Images'
);
public function beforeFilter() {
$this->Auth->allow('index', 'display', 'view');
}
}
<div>
<header><?php echo __('usersaccessform');?></header>
<?php
echo $this->Form->create('User');
echo $this->Form->input('User.username',array('type'=>'text'));
echo $this->Form->input('User.password',array('type'=>'password'));
echo $this->Form->end(__('submit'));
?>
</div>
答案 0 :(得分:0)
问题在于你的密码加密,看看你是否正在进行之前的保存并且在保存时进行加密就像你正在对你登录的密码和密码进行双重加密以验证它们是否存在在您的数据库中是相同的
UsersController
public function beforeFilter() {
parent::beforeFilter();
}
public function login() {
if($this->Session->read('Auth.User')){
$this->redirect($this->Auth->redirect());
}
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
用户模型
public function beforeSave($options = array()) {
// hash our password
if (!$this->id) {
$passwordHasher = new SimplePasswordHasher();
$this->data['User']['password'] = $passwordHasher->hash($this->data['User']['password']);
}
return true;
}
AppController的
public function beforeFilter() {
Security::setHash('sha1');
$this->Auth->allow('index', 'display', 'view');
}
在您的数据库中,密码字段是varchar(30)