Cakephp中的登录函数

Cakephp中的登录函数,php,cakephp,Php,Cakephp,我是快速开发框架的新手,目前正在从事一个关于Cakephp的项目。我在申请建立会话时遇到问题。 我使用了登录功能,但它不会接受我输入的凭据,并返回错误凭据的闪存消息。我尝试过以不同的方式改变功能,但很明显,这不是建立会话。请帮忙 这是相关代码 UsersController.php public function login() { if ($this->request->is('post')) { if ( $this->request->is(

我是快速开发框架的新手,目前正在从事一个关于Cakephp的项目。我在申请建立会话时遇到问题。 我使用了登录功能,但它不会接受我输入的凭据,并返回错误凭据的闪存消息。我尝试过以不同的方式改变功能,但很明显,这不是建立会话。请帮忙

这是相关代码

UsersController.php

public function login() {
    if ($this->request->is('post')) {
        if ( $this->request->is( 'post' ) ) {
            if ( $this->Auth->login() ) {
               $this->redirect($this->Auth->redirect());
            } else {
               $this->Session->setFlash(__('Your username or password was incorrect.'));
            }
        }
    }
}
public $components = array('Session', 'Auth');
AppController.php

public function login() {
    if ($this->request->is('post')) {
        if ( $this->request->is( 'post' ) ) {
            if ( $this->Auth->login() ) {
               $this->redirect($this->Auth->redirect());
            } else {
               $this->Session->setFlash(__('Your username or password was incorrect.'));
            }
        }
    }
}
public $components = array('Session', 'Auth');

您的AppController似乎在
$components
中缺少一些信息
试试这个:

class AppController extends Controller {

public $components = array(
    'Cookie',
    'Session',
    'Auth' => array(
        'loginRedirect' => array(
            'controller' => '**YOUR CONTOLLERr**',
            'action' => '**YOUR ACTION**'
        ),
        'logoutRedirect' => array(
            'controller' => '**YOUR CONTOLLERr**',
            'action' => '**YOUR ACTION**',
            'home'
        ),
        'loginAction' => array(
        'controller' => '**YOUR CONTOLLERr**',
        'action' => '**YOUR ACTION**'
        ),
        'authError' => 'Access Denied!',
        'loginError' => 'Invalid user and password',
        'authorize' => array('Controller'),
        'authenticate' => array('Form' => array(
                                'userModel' => 'User',
                                'fields' => array(
                                    'username' => '**LOGIN FIELD**',
                                    'password' => '**PASSWORD FIELD**'
                                    )
                                )
        ),
    )
);
}
在AppController内设置
会话
非常重要,这样您就可以建立会话。 另外,检查密码散列也很重要,因为Cakephp仅在数据库中存储的密码被散列时验证凭据

希望它对您有所帮助。

我在您的“相关代码”中看不到任何内容,您可以将您获得的数据与登录时的数据库进行比较,只需使用debug即可,因此:

public function login() {
        if ($this->request->is('post')) {
            debug($this->request->data);
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                //$this->Session->setFlash(('Your username or password was incorrect.'));
            }
        }
      }

有了这些,我们可以获得更多信息,您可以告诉我显示了什么,以及这些数据在数据库中是否相同。

嘿,谢谢您的反馈。我已经设置了会话,我刚刚将$components方法修改为您提供的代码,但结果仍然相同。可能缺少什么?您需要根据您的应用程序调整LoginDirect、LoginDirect和loginAction。但是为什么要用
if($this->request->is('post'){
)两次验证提交表单呢?此外,在userscocontroller.php中包含这一行并检查它是否有效:
App::uses('CakeSession','Model/Datasource'));
没有任何东西会因为你的代码不正确而突然出现在我面前。我会打开debug并获取一个mysql转储,以查看它用于登录的实际查询。这是查询选择
用户
id
用户
类型id
用户
用户名
用户
密码
用户
>从
mdbk
用户
修改为
User
其中
User
用户名
用户名
限制1您确定这是用于登录的查询吗?它没有与密码字段进行比较。因此,要么是查询错误,要么是其他问题,尽管问题不在您输入的代码中ted。我该如何调试它?我会先看一下你的Auth组件的设置。如果没有发现任何错误,那么就开始检查CakePHP库,看看你是否能找出让它比较密码的代码在哪里,以及为什么它没有到达那里。我通常只是临时添加大量调用
debug()
在各种变量上执行此操作。嘿,这是我得到的结果数组('User'=>array('password'=>'***','username'=>'test'))对密码进行调试,并比较它是否与数据库中的密码相同。我不知道如何执行此操作。让我尝试一下。谢谢。