Cakephp中的登录功能

时间:2015-01-20 15:25:43

标签: php cakephp

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

以下是相关代码。

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.'));
            }
        }
    }
}

AppController.php

public $components = array('Session', 'Auth');

2 个答案:

答案 0 :(得分:0)

您的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中设置Session非常重要,这样您就可以建立它。 检查 PasswordHash 也很重要,因为如果存储在数据库中的密码是哈希值,Cakephp只会验证凭据。

希望它可以帮助你。

答案 1 :(得分:0)

我的相关代码"中没有任何内容,您可以在登录时将您获得的数据与数据库进行比较,只需使用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.'));
            }
        }
      }

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