哈希密码未在 cakephp 中更新后

After hash password not updating in cakephp

我正在尝试使用 ajax 来更改密码选项。在这里,我在控制器

中尝试了以下代码
       if ($this->request->is(array('post', 'put'))){
            $id=$this->Auth->user('id'); 
            $changePass=$_POST['cpass'];
                $cpasss=AuthComponent::password($changePass);
                echo $cpasss;
                $up=$this->User->updateAll(
                array('User.password'=>"$cpasss"),
                array('User.id'=>"$id")
            );      
        }

这里的哈希工作正常,我在 firebug 看到了,但问题是密码在 database.If 中没有更新 我删除了

$cpasss=AuthComponent::password($changePass);   

然后密码更新正常,但没有 hash.May 有人帮我解决这个问题吗?

将您的代码更新为:

   if ($this->request->is(array('post', 'put'))){
        $id=$this->Auth->user('id'); 
        $changePass= $this->request->data['cpass'];
            $cpasss=AuthComponent::password($changePass);

            $up=$this->User->updateAll(
            array('User.password'=> $cpasss),
            array('User.id'=> $id)
        );      
    }

更改

  1. $_POST[] 不是在 cakephp 中获取表单数据的正确方法。
  2. 无需将变量包含在双引号中(")。

这里不用updateAll()。相反,只需使用 save() 保存数据并记住将主键与保存数据一起传递:-

if ($this->request->is(array('post', 'put'))) {
    $id = $this->Auth->user('id');
    $changePass = $this->request->data['User']['cpass'];

    $data = array(
        'id' => $id,
        'password' => AuthComponent::password($changePass)
    );
    $this->User->save($data);
}

如果您确实使用了 updateAll(),则需要谨慎使用,因为它不会转义传递的字段。来自 the docs:-

The $fields array accepts SQL expressions. Literal values should be quoted manually using DboSource::value().