【问题标题】:cakephp error: invalid username and password,try againcakephp 错误:用户名和密码无效,请重试
【发布时间】:2014-11-28 07:00:18
【问题描述】:

我坚持认为我的 cakephp 登录不起作用。我正在同一视图上创建登录和注册。期待帮助。这是我的代码。

     public function login(){
         (isset($this->request->data['User'])) { // Check if the login Form was submitted
                  if (!empty($this->request->data['User']) && $this->Auth->login()) {

                 return $this->redirect($this->Auth->redirect());
                    }
                    $this->request->data['User']['password'] = '';
    $this->Session->setFlash(__('Invalid username or password, try again'));
}

}

这个函数的模型是

    class User extends AppModel{
     public $validate = array(
     'email'=>'email',
    'password' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'A password is required'
        )
    ),);

观点是:

      <div><?php echo $this->Session->flash(); ?></div>
      <?php echo $this->Form->create("User");  ?>
     <?php echo $this->Form->input('email' ,array('label'=>"Email :")); ?>
     <?php echo $this->Form->input('password',array('label'=>"Password"));?>
     <?php echo $this->Form->end('Login'); ?>

哈希密码使用注册正确保存在数据库中,但是当我在登录时输入密码时,它给出了无效的用户名和密码错误。 我的 AppController 是

 class AppController extends Controller {
         public $components = array(
       'Auth'=> array(
        'authenticate' => array(
        'Form' => array(
            'fields' => array('username' =>'email','password'=>'password')
        )
    )
),
    'Session',
    'Auth' => array(
        'loginRedirect' => array(
            'controller' => 'users',
            'action' => 'profileindex'
        ),
        //'authorize' => array('Controller'),
                'loginAction' => array('controller' => 'users', 'action' => 'login'),
                'loginRedirect' => array('controller' => 'users', 'action' => 'profileindex'),
                'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
                'authError' => 'You don\'t have access here.',

        'logoutRedirect' => array(
            'controller' => 'users',
            'action' => 'login',
            'home'
        ),
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'Blowfish'
            )
        )
    )
);

【问题讨论】:

  • 首先在数据库中使用加密形式的密码。
  • 密码加密存储在数据库中。
  • 我已经做到了兄弟,我是这个 cakephp 的新手,所以我想我错过了一些东西。
  • 遇到同样的问题你找到解决办法了吗

标签: cakephp


【解决方案1】:
<?php
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {

    public function beforeSave($options = array()) {


            $passwordHasher = new BlowfishPasswordHasher();
            $this->data[$this->alias]['password'] = $passwordHasher->hash($this->data[$this->alias]['password']);

        return true;
    }
}

【讨论】:

  • 它在白色浏览器页面中返回“false”而不进入索引页面。
  • $this->request->data['User'] 是否具有用户名和密码的值?
  • (isset($this->request->data['User'])) { // 检查登录表单是否提交 if ($this->Auth->login($this- >request->data['User'] )) { return $this->redirect($this->Auth->redirect()); }
  • 如果它不起作用,您必须检查您的应用控制器。
  • 它还没有工作。我在上面添加的是我的AppController,你可以看看它吗?
猜你喜欢
  • 2012-06-08
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
  • 2012-07-18
  • 2016-08-22
  • 2013-06-03
  • 2020-03-29
  • 1970-01-01
相关资源
最近更新 更多