cakephp。如何将密码与数据库中的密码进行比较

cakephp。如何将密码与数据库中的密码进行比较,php,cakephp,passwords,Php,Cakephp,Passwords,因为几天,差不多一个星期,我遇到了一个问题,用cakephp比较密码 我正在准备编辑用户视图,在用户能够更改当前密码之前,他需要键入旧密码。(我正在扩展cakebook的授权教程。) 用户在创建密码时正在进行哈希运算 在User.php(Model)中 我尝试将AuthComponent之后的字段old_password和(接收用户通行证的任何方式)相比较,比如$this->Session->read('Auth.user.password'),但当然失败了,我尝试发送old_password并

因为几天,差不多一个星期,我遇到了一个问题,用cakephp比较密码

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

用户在创建密码时正在进行哈希运算 在User.php(Model)中

我尝试将AuthComponent之后的字段old_password和(接收用户通行证的任何方式)
相比较,比如$this->Session->read('Auth.user.password')
,但当然失败了,我尝试发送old_password并在model 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'
我还尝试将旧的_密码输入与edit.ctp中的数据库1进行比较,但我所有的工作都失败了

请给我一些小费


编辑(由于我的声誉不高,我无法在发帖后8小时内回复自己的帖子,因此我将此部分编辑为)


阿尼尔·库马尔你给了我很好的建议。我放弃了你的方式,但是发生了一个内部错误。 错误:发生内部错误。 每一次,我都会在路上更改这部分代码,就像法洛一样,它完美地工作了,当然要感谢你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;
}

为旧密码定义规则

'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..
}

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

/**
     * 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;
}
你可能想调查一下。作为行为的一部分,您可以将
current
设置为true。
/**
     * 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;
}