Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/cakephp/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP 2.x身份验证自定义用户名(代码到位但不起作用!)_Cakephp_Authentication - Fatal编程技术网

CakePHP 2.x身份验证自定义用户名(代码到位但不起作用!)

CakePHP 2.x身份验证自定义用户名(代码到位但不起作用!),cakephp,authentication,Cakephp,Authentication,对不起,我不想问,但我已经花了一个小时的时间来解决这个问题和研究,但没有任何运气 CakePHP(运行最新版本)似乎拒绝使用字段设置(这样我就可以使用数据库中的email列作为用户名)。如果我将其设置为“email”,这是我希望从数据库中使用的字段,它只会拒绝登录并声明错误的详细信息。由于某些原因,无法从DebugKit中的SQL获取任何输出。虽然当它被设置为username时,只要在数据库中使用一个“temp”列就可以了。我试着把它放在组件变量中,但也没有成功。我可能做错了什么?调试已打开,在

对不起,我不想问,但我已经花了一个小时的时间来解决这个问题和研究,但没有任何运气

CakePHP(运行最新版本)似乎拒绝使用字段设置(这样我就可以使用数据库中的email列作为用户名)。如果我将其设置为“email”,这是我希望从数据库中使用的字段,它只会拒绝登录并声明错误的详细信息。由于某些原因,无法从DebugKit中的SQL获取任何输出。虽然当它被设置为username时,只要在数据库中使用一个“temp”列就可以了。我试着把它放在组件变量中,但也没有成功。我可能做错了什么?调试已打开,在日志或浏览器中看不到任何错误

该模型确实包含电子邮件列

Controller/AppController.php

class AppController extends Controller {

        public $components = array(
                'Session',
                'DebugKit.Toolbar',
                'Auth' => array(
                        'allow' => array('login','logout'),
                        'loginAction' => array('controller' => 'users', 'action' => 'login'),
                        'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
                        'authorize' => 'Controller'
                )
        );

        function beforeFilter() {
                Security::setHash('md5');
                $this->Auth->authenticate = array(
                        'Form' => array(
                                'fields' => array(
                                        'username' => 'username',
                                ),
                        ),
                );
        }
}
class UsersController extends AppController {

        public $uses = array('User');

        public function beforeFilter() {
                parent::beforeFilter();
        }

        public function isAuthorized($user){
                return true;
        }

        public function login() {
                $this->layout = 'login';

                if ($this->request->is('post')) {
                        if ($this->Auth->login()) {
                                $this->redirect($this->Auth->redirect());
                        } else {
                                $this->Session->setFlash('Invalid username or password, try again','flash_error');
                        }
                }
        }

        public function logout() {
                $this->layout = 'login';

                $this->Session->setFlash('Successfully logged out!','flash_success');
                $this->redirect($this->Auth->logout());
        }

}
<?php
        $this->set('title_for_layout', 'Login');

        echo $this->Session->flash();
        echo $this->Session->flash('auth','flash_info');

        echo $this->Form->create('User', array(
                'action' => 'login'
        ));
        echo $this->Form->input('username',array(
                'between' => '<br/>',
                'before' => '<p>',
                'after' => '</p>',
                'class' => 'text',
                'label' => 'Email:'
        ));
        echo $this->Form->input('password',array(
                'between' => '<br/>',
                'before' => '<p>',
                'after' => '</p>',
                'class' => 'text',
                'label' => 'Password:'
        ));
        echo $this->Form->submit('Login', array(
                'class' => 'submit',
                'before' => '<p>',
                'after' => '</p>'
        ));
        echo $this->Form->end();
?>
Controller/UserController.php

class AppController extends Controller {

        public $components = array(
                'Session',
                'DebugKit.Toolbar',
                'Auth' => array(
                        'allow' => array('login','logout'),
                        'loginAction' => array('controller' => 'users', 'action' => 'login'),
                        'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
                        'authorize' => 'Controller'
                )
        );

        function beforeFilter() {
                Security::setHash('md5');
                $this->Auth->authenticate = array(
                        'Form' => array(
                                'fields' => array(
                                        'username' => 'username',
                                ),
                        ),
                );
        }
}
class UsersController extends AppController {

        public $uses = array('User');

        public function beforeFilter() {
                parent::beforeFilter();
        }

        public function isAuthorized($user){
                return true;
        }

        public function login() {
                $this->layout = 'login';

                if ($this->request->is('post')) {
                        if ($this->Auth->login()) {
                                $this->redirect($this->Auth->redirect());
                        } else {
                                $this->Session->setFlash('Invalid username or password, try again','flash_error');
                        }
                }
        }

        public function logout() {
                $this->layout = 'login';

                $this->Session->setFlash('Successfully logged out!','flash_success');
                $this->redirect($this->Auth->logout());
        }

}
<?php
        $this->set('title_for_layout', 'Login');

        echo $this->Session->flash();
        echo $this->Session->flash('auth','flash_info');

        echo $this->Form->create('User', array(
                'action' => 'login'
        ));
        echo $this->Form->input('username',array(
                'between' => '<br/>',
                'before' => '<p>',
                'after' => '</p>',
                'class' => 'text',
                'label' => 'Email:'
        ));
        echo $this->Form->input('password',array(
                'between' => '<br/>',
                'before' => '<p>',
                'after' => '</p>',
                'class' => 'text',
                'label' => 'Password:'
        ));
        echo $this->Form->submit('Login', array(
                'class' => 'submit',
                'before' => '<p>',
                'after' => '</p>'
        ));
        echo $this->Form->end();
?>
查看/用户/登录.ctp

class AppController extends Controller {

        public $components = array(
                'Session',
                'DebugKit.Toolbar',
                'Auth' => array(
                        'allow' => array('login','logout'),
                        'loginAction' => array('controller' => 'users', 'action' => 'login'),
                        'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
                        'authorize' => 'Controller'
                )
        );

        function beforeFilter() {
                Security::setHash('md5');
                $this->Auth->authenticate = array(
                        'Form' => array(
                                'fields' => array(
                                        'username' => 'username',
                                ),
                        ),
                );
        }
}
class UsersController extends AppController {

        public $uses = array('User');

        public function beforeFilter() {
                parent::beforeFilter();
        }

        public function isAuthorized($user){
                return true;
        }

        public function login() {
                $this->layout = 'login';

                if ($this->request->is('post')) {
                        if ($this->Auth->login()) {
                                $this->redirect($this->Auth->redirect());
                        } else {
                                $this->Session->setFlash('Invalid username or password, try again','flash_error');
                        }
                }
        }

        public function logout() {
                $this->layout = 'login';

                $this->Session->setFlash('Successfully logged out!','flash_success');
                $this->redirect($this->Auth->logout());
        }

}
<?php
        $this->set('title_for_layout', 'Login');

        echo $this->Session->flash();
        echo $this->Session->flash('auth','flash_info');

        echo $this->Form->create('User', array(
                'action' => 'login'
        ));
        echo $this->Form->input('username',array(
                'between' => '<br/>',
                'before' => '<p>',
                'after' => '</p>',
                'class' => 'text',
                'label' => 'Email:'
        ));
        echo $this->Form->input('password',array(
                'between' => '<br/>',
                'before' => '<p>',
                'after' => '</p>',
                'class' => 'text',
                'label' => 'Password:'
        ));
        echo $this->Form->submit('Login', array(
                'class' => 'submit',
                'before' => '<p>',
                'after' => '</p>'
        ));
        echo $this->Form->end();
?>

尝试更新appController中的组件代码,将身份验证值添加到身份验证数组,如下所示:

public $components = array(
            'Session',
            'DebugKit.Toolbar',
            'Auth' => array(
                    'allow' => array('login','logout'),
                    'loginAction' => array('controller' => 'users', 'action' => 'login'),
                    'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
                    'authorize' => 'Controller',
                    'authenticate' => array(
                        'Form' => array(
                                'fields' => array('username' => 'email')
                            )
                    )
            )
    );

尝试更新appController中的组件代码,以将身份验证值添加到身份验证数组,如下所示:

public $components = array(
            'Session',
            'DebugKit.Toolbar',
            'Auth' => array(
                    'allow' => array('login','logout'),
                    'loginAction' => array('controller' => 'users', 'action' => 'login'),
                    'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
                    'authorize' => 'Controller',
                    'authenticate' => array(
                        'Form' => array(
                                'fields' => array('username' => 'email')
                            )
                    )
            )
    );

您需要将表单上字段的名称从用户名更改为电子邮件。仅仅将标签设置为“电子邮件”是不够的

        echo $this->Form->input('email',array(
            'between' => '<br/>',
            'before' => '<p>',
            'after' => '</p>',
            'class' => 'text',
            'label' => 'Email:'
echo$this->Form->input('email',数组(
'在'=>'
'之间, '在'=>''之前, '在'=>'之后

', “类”=>“文本”, '标签'=>'电子邮件:'
您需要将表单上字段的名称从用户名更改为电子邮件。仅将标签设置为“电子邮件”是不够的

        echo $this->Form->input('email',array(
            'between' => '<br/>',
            'before' => '<p>',
            'after' => '</p>',
            'class' => 'text',
            'label' => 'Email:'
echo$this->Form->input('email',数组(
'在'=>'
'之间, '在'=>''之前, '在'=>'之后

', “类”=>“文本”, '标签'=>'电子邮件:'
谢谢你的建议法案。不幸的是,我已经尝试过了,它与上面的代码具有相同的效果。我只是再次尝试从上面的代码复制authenticate数组,以确保它不是我的拼写,但仍然没有运气!我很乐意尝试调试…但我找不到任何要调试的日志:(感谢您的建议法案。不幸的是,我已经尝试过了,它与上面的代码具有相同的效果。我只是再次尝试从上面的代码复制authenticate数组,以确保它不是我的拼写,但仍然不走运!我很乐意尝试调试…但我找不到任何要调试的日志:(可以发誓我试过了…一定是和其他东西结合在一起导致它原本不起作用。现在一切正常。谢谢:)可以发誓我试过了…一定是和其他东西结合在一起导致它原本不起作用。现在一切正常。谢谢:)