这是代码
UsersController.php
<?php
class UsersController extends AppController
{
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'));
}
}
public function logout()
{
return $this->redirect($this->Auth->logout());
}
}
?>
User.php
(模型文件)
<?php
class User extends AppModel
{
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;
}
}
login.ctp
(查看文件)
<?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'));
?>
的 AppController.php
<?php
class AppController extends Controller
{
public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'books',
'action' => 'view'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'home'
),
)
);
}
表名:用户
我是CakePHP的新手,我刚刚编写了简单登录操作的代码,
当我尝试使用正确的凭据登录时,它始终显示不正确的用户名和密码
$this->Auth->login()
总是返回false值,即使我提供了正确的用户名和密码。请给我一个解决方案。谢谢。
答案 0 :(得分:2)
你应该试试这个
在CakePHP数据库中,表名应为复数。模型名称应该是CamelCase的单数。控制器应为复数,并且必须添加CamelCase添加后缀 Controller 。查看文件夹名称始终与Controller复数名称(不带后缀)相同,
.ctp
文件名应为动词。
<强> AppController.php 强>
<?php
class AppController extends Controller {
public $components = array(
'RequestHandler','Session',
'Auth' => array(
'Autoredirect'=>false,
'loginRedirect' => array('controller' => 'books', 'action' => 'view'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home'),
'authError' => 'Did you really think you are allowed to see that?',
)
);
}
?>
<强> UsersController.php 强>
<?php
class UsersController extends AppController
{
var $helpers = array('Html', 'Form','Js','Session');
public $components = array(
'RequestHandler',
'Session'
);
public function login(){
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->Session->setFlash('You are successfully login','default',array('class'=>'alert alert-success'));
return $this->redirect($this->Auth->redirect());
}
return $this->Session->setFlash('Invalid username and password,please try again','default',array('class'=>'alert alert-danger'));
}
}
}
?>
<强> login.ctp 强>
<?php echo $this->Session->flash(); ?>
<?php echo $this->Form->create('User'); ?>
<div class="form-group">
<label>Email</label>
<?php echo $this->Form->input('User.username');?>
</div>
<div class="form-group">
<label>Password</label>
<?php echo $this->Form->input('User.password');?>
</div>
<div class="form-group">
<?php echo $this->Form->button('Login',array('type'=>'submit','class'=>'btn btn-primary btn-block')); ?>
</div>
<?php echo $this->Form->end(); ?>
<强> AppModel.php 强>
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
class AppModel extends Model {
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'passwordHasher' => array(
'className' => 'Simple',
'hashType' => 'sha1'
)
)
)
)
);
public function beforeSave($options = array()) {
if (!empty($this->data[$this->alias]['password'])) {
$passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha1'));
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
return true;
}
}
?>
User.php(用户模型)
<?php
class User extends AppModel
{
public $validate = array(
'username' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'Letters and numbers only'
),
),
'password' => array(
'rule' => array('minLength', '8'),
'message' => 'Minimum 8 characters long'
),
);
}
?>
答案 1 :(得分:0)
无意中我限制了数据库中的密码字段,以便密码部分存储在数据库中,这就是导致此问题的原因。