簡體   English   中英

Cakephp登錄表單中的密碼或用戶名不正確

[英]Incorrect password or username in login form in cakephp

我不知道在擁有所有代碼和數據庫的情況下為什么仍然會獲得無效的用戶名或密碼。 用戶也已經注冊但無法登錄。

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;
    } 
}

控制器: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');

    ?>

您沒有使用CakePHP期望的默認字段名稱。 因此,您必須將它們包括在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'
                    ),
                ),
            )
        )
    );
}

這些名稱應與users表和視圖中的字段名稱匹配。

編輯

另外,請確保數據庫中的密碼字段足夠長以存儲哈希密碼。 CakePHP使用的默認算法是SHA1,它會產生160位(40個字符)的哈希。 VARCHAR 255應該足以存儲此字符串。

看到:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM