【问题标题】:Cakephp 2.4 AuthenticationCakephp 2.4 身份验证
【发布时间】:2014-05-11 20:49:43
【问题描述】:

我使用的是 Cakephp 2.4,但我在使用 Auth 进行简单身份验证时遇到了问题。

我使用数据库中的“邮件”列作为登录名。

我做什么:

在 AppController.php 中

public $components = array('DebugKit.Toolbar', 'Session', 'Auth' => array(
        'loginAction' => array(
            'controller' => 'users',
            'action' => 'login',
            'plugin' => false
        ),
        'authError' => 'Pensiez-vous réellement que vous étiez autorisés à voir cela ?',
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'mail')
            )
        )
    ));

在 UsersController.php 中

public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                $this->Session->setFlash(
                    __('Username ou password est incorrect'),
                    'default',
                    array(),
                    'auth'
                );
            }
        }
    }

在 login.ctp

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User');?>
    <fieldset>
        <legend><?php echo __('Merci de rentrer votre nom d\'user et mot de passe'); ?></legend>
        <?php echo $this->Form->input('username');
        echo $this->Form->input('password');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Connexion'));?>
</div>

我尝试使用清晰的散列密码,但每次他都不让我登录。

【问题讨论】:

  • 大声笑,我只是尝试一些新的东西,我给登录功能 $this->request->data 并且他做一些事情,如果有人可以告诉我这是否是好方法?跨度>
  • 好吧,这可能不是好方法,因为登录功能对每个密码都返回 true。

标签: php cakephp authentication cakephp-2.4


【解决方案1】:

在您看来,您需要使用字段名称“邮件”而不是“用户名”。

echo $this->Form->input('mail');

【讨论】:

    【解决方案2】:

    看起来您使用了错误的表单域...这是我的代码,它在我的一个应用程序中运行:

    控制器:

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

    查看:

    echo $this->Form->create();
        echo $this->Form->input('email');
        echo $this->Form->input('password');
    echo $this->Form->end(__('Sign in', true));
    

    请注意,我在表单中使用的是“电子邮件”输入,而不是错误的“用户名”。

    【讨论】:

    • 在 $component 中,表单字段没有注意到他需要用作用户名的字段?在我的情况下是“邮件”
    • 确实如此,它是“电子邮件”字段。只需将我的“电子邮件”重命名为您的“邮件”即可。
    • 好的,当我发现我犯了另一个错误时,我会尝试,因为现在 cakephp 登录函数每次都返回 true,我可能在某个地方尝试了一些错误:p
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2011-03-11
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多