HTML   发布时间:2022-04-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了表单 – 无法使用Auth登录与CakePHP 2.0一起使用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用Cake PHP 2.0获得一个简单的登录表单…只是Auth,现在没有ACl.

我能够看到表单并输入电子邮件和密码,因为它们在数据库中,但我只是返回到表单并显示flash错误消息.这是我的代码:

AppController的:

class AppController extends Controller
 {
     function beforeFilter()
     {
         $this->Auth->userModel = 'Users';
         $this->Auth->fields = array('username' => 'email','password' => 'password'); //have to put both,even if we're just changing one
         $this->Auth->loginAction = array('controller' => 'users','action' => 'login');
         $this->Auth->loginRedirect = array('controller' => 'hotels','action' => 'dashboard');
         $this->Auth->logoutRedirect = array('controller' => 'users','action' => 'login');
     }
 }

login.ctp:

<?php
         echo $this->Form->create('User',array('action' => 'login'));
         echo $this->Form->input('email');
         echo $this->Form->input('password');
         echo $this->Form->end('Login');
     ?>

UsersController:

class UsersController extends AppController
 {
     var $name = 'Users';
     var $Helpers = array('Html','Form');
     var $components = array('Auth','Session');

     function beforeFilter()
     {
         $this->Auth->allow("logout");
         parent::beforeFilter();
     }

     function index() { } //Redirects to login()

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

     function logout()
     {
         $this->redirect($this->Auth->logout());
     }
 }
 ?>

我很感激任何帮助.谢谢!

解决方法

登录后会显示“无效的用户名或密码,再试一次”错误?

你应该检查几件事:

•$this-> Auth-> login()的输出是否与数据库中的信息相同?调试($this-> Auth-> login())以在提交表单后查看login方法中的输出.

•密码是否在数据库中正确散列?

•尝试使AuthComponent对所有控制器可用,而不仅仅是UsersController.

•不确定这是否有所不同,但请调用parent :: beforeFilter();在控制器的beforeFilter方法之前的任何其他内容之前.

编辑:

是否正在尝试根据电子邮件和密码进行验证.默认情况下,AuthComponent需要用户名和密码.您必须明确声明您希望通过$this-> Auth-> login()验证电子邮件和密码.这来自2.0文档:

public $components = array(
    'Auth'=> array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )
    )
);

我相信,你没有看到任何SQL输出这一事实.

大佬总结

以上是大佬教程为你收集整理的表单 – 无法使用Auth登录与CakePHP 2.0一起使用全部内容,希望文章能够帮你解决表单 – 无法使用Auth登录与CakePHP 2.0一起使用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。