编程语言
首页 > 编程语言> > php – 仅填写字段时才更新密码

php – 仅填写字段时才更新密码

作者:互联网

我的看法:

  <tr>
    <td>Username</td>
    <td><?php echo $this->Form->input('User.username', array('label' => '')); ?></td>
  </tr>
  <tr>
    <td>Password</td>
    <td><?php echo $this->Form->password('User.password', array('label' => '', 'value'=>'')); ?></td>
  </tr>

我的控制器:

    function edit($id = null) {

        $this->User->id = $id;

        $data = $this->data;
#       print_r($data);

        if (empty($data)) {
            $this->data = $this->User->read();
        } else {
            if ($this->User->save($data)) {
                $this->Session->setFlash('The user details have been updated.');
                $this->redirect(array('action' => 'index'));
            }
        }

    }

在我使用空白密码提交表单时,数据库中的哈希值仍会发生变化.如果填写了新密码,我该如何才更新密码哈希.

谢谢.

编辑:$data [‘用户’] [‘密码’]始终是一个哈希,永远不会空!

解决方法:

CakePHP 1.3自动散列密码字段. CakePHP 2.0没有.

您有几个选择(在我看来,从最差到最好):

>重命名您的字段并在保存之前进行交换

if ($data['User']['new_password'] != '') {
    $data['User']['password'] = $this->Auth->password($data['User']['new_password']);
}

>在等式检查中散列字符串

if ($data['User']['password'] == $this->Auth->password('')) {
    unset($data['User']['password']);
}

>将散列函数更改为不散列空密码的函数[请参阅book以获取配置]:

function hashPasswords($data) {
    if (!empty($this->data['User']['password'])) {
        $this->data['User']['password'] = $this->Auth->password($this->data['User']['password']);
    }
    return $data;
}

标签:cakephp-1-3,php,cakephp
来源: https://codeday.me/bug/20190729/1575758.html