gpt4 book ai didi

forms - 无法获得 Auth 登录以使用 CakePHP 2.0

转载 作者:行者123 更新时间:2023-12-04 10:05:21 27 4
gpt4 key购买 nike

我正在尝试使用 CakePHP 2.0 获得一个简单的登录表单......只是身份验证,现在没有 ACL。

我能够看到表单并输入电子邮件和密码,就像它们在数据库中一样,但我只是返回到表单并显示闪存错误消息。这是我的代码:

应用 Controller :

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

登录.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');
?>

用户 Controller :

 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() 的输出与您数据库中的信息相同吗?把 debug($this->Auth->login())在提交表单后查看登录方法中的输出。

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

• 尝试制作 AuthComponent可用于您的所有 Controller ,而不仅仅是 UsersController .

• 不确定这是否有影响,但请调用 parent::beforeFilter();在您的 Controller beforeFilter 中的任何其他内容之前方法。

编辑:

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

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

我相信,您没有看到任何 SQL 输出是意料之中的事实。

关于forms - 无法获得 Auth 登录以使用 CakePHP 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8024831/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com