【问题标题】:cakephp. how to compare password with password in database蛋糕PHP。如何将密码与数据库中的密码进行比较
【发布时间】:2013-11-07 10:43:58
【问题描述】:

几天以来,差不多一个星期,我有一个问题,要比较 cakephp 中的密码。

我正在准备编辑用户视图,在用户能够更改他的当前密码之前,他需要输入他的旧密码。 (我正在从 cakebook 扩展授权教程。)

当用户创建他的密码时,正在散列 在 User.php(模型)中

public function beforeSave($options = array()) {
    {
        if(isset($this->data[$this->alias]['password']))
        {
                $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
}

我尝试在 AuthComponent 之后将字段 old_password 与(接收用户通行证的任何方式)like $this->Session->read('Auth.User.password') 进行比较,但当然它失败了,我尝试发送 old_password 并将其散列在模型 User.php 中,我也在这个模型中创建

App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel
{
public $validate = array(
    'username'=>array(
        'required'=>array(
            'rule'=>array('notEmpty'),
            'message'=>'Write correct Login'
        )
    ),
    'password'=>array(
        'required'=>array(
            'rule'=>array('notEmpty'),
            'message'=>'Please re-enter your password twice so that the values match'
        )
    ),
    'old_password'=>array(
        'required'=>array(
            'rule'=>array('equalTo'=>'password'),
            'message'=>'Wrong'
        )
    )
);

使用'equalTo',password'equalTo','password' 的不同方式 我还尝试将 old_password 输入与 edit.ctp 中的数据库一进行比较,但我所有的工作都失败了。

请给我一些提示。


编辑(由于我的声誉低下,我无法在询问后 8 小时之前回复我自己的帖子,因此我将这部分编辑为)


Anil Kumar 你给了我很好的建议。我不喜欢你,但是An Internal Error Has Occurred. Error: An Internal Error Has Occurred. 每次,我都会在途中更改这部分代码,作为休闲,它完美地工作,当然感谢 You Anil Kumar。

public function password_verifies()
{
    //$this->User->id = $this->data[$this->alias]['id'];
    //return AuthComponent::password($this->data[$this->alias]['password']) == $this->User->field('password');
    $od = AuthComponent::password($this->data['User']['old_password']);
    if($od == $this->field('password'))
    {
        return true;
    }
    return false;
}

【问题讨论】:

  • 您可能想查看this。作为行为的一部分,您可以将 current 设置为 true。

标签: php cakephp passwords


【解决方案1】:

定义旧密码规则

'old_password' => array(
    'rule' => 'password_verifies',
    'message' => 'wrong'
)

验证密码匹配的功能

public function password_verifies() {
    // getting password via field method by assuming you're setting $this->User->id from your controller
    return AuthComponent::password($this->data[$this->alias]['password']) == $this->User->field('password'); 
}

从控制器验证

在您的控制器中,您必须在验证之前设置 id。

$this->User->id = $this->Auth->user('id');
$this->User->set($this->request->data);
if ($this->User->validates()) {
    // do your stuff..
}

【讨论】:

    【解决方案2】:

    以下是我的网站比较密码的工作示例,

    /**
         * Validation rules
         * @var array
         */
        var $validate = array(
            'changepass' => array(
                'notEmpty' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Please insert Password',
                    'last' => true
                ),
                'minLength' => array(
                    'rule' => array('minLength', 8),
                    'message' => 'Your password must be at least 8 characters long',
                    'last' => true
                )
            ),
            'checkpassword' => array(
                'notEmpty' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Please insert Confirm Password',
                    'last' => true
                ),
                'checkValue' => array(
                    'rule' => array('comparePassword', 'changepass'),
                    'message' => 'Please enter the same password as above',
                    'last' => true
                )
            )
        );
    
    // Validating the values of two fields to not to be identical to each other
    function comparePassword($field = array(), $compareField = null) {
        foreach ($field as $key => $value) {
            $v1 = $value;
            $v2 = $this->data[$this->name][$compareField];
    
            if ($v1 != $v2)
                return false;
            else
                continue;
        }
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 2011-03-09
      • 1970-01-01
      • 2016-03-06
      • 2021-09-27
      • 1970-01-01
      相关资源
      最近更新 更多