【问题标题】:How to update password field through CakePHP form如何通过 CakePHP 表单更新密码字段
【发布时间】:2015-07-02 11:09:54
【问题描述】:

我正在使用以下代码。但它不会更新为哈希值。只是保存为输入值。请帮忙。

$this->request->data['User']['password'] = md5($this->request->data['User']['password']);
if ($this->User->updateAll(array('User.password' => $this->request->data['User']['password']),array('User.id' => $newsid))) {
$this->Session->setFlash("Password Changed");
$this->redirect(array('controller' => 'admins', 'action' => 'login'));  
          } else {
            $this->Session->setFlash("password not changed");
            $this->render();
        }

【问题讨论】:

  • 您好,在 cake php 中,密码字段不需要 md5,您在 cakephp 中使用什么版本?
  • 但是通过这段代码。如何将密码保存为哈希值?
  • 你遇到了什么错误? ,你需要更新一行还是所有行?
  • Column not found: 1054 Unknown column 'c920c1595dcdb710a8cbd045b9735acb' in 'field list' UPDATE demo.users AS User SET User.password = c920c1595dcdb710a8cbd045b9735acb WHERE User. id = 7

标签: cakephp hash md5


【解决方案1】:

在模型中保存之前:

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

控制器文件:

public function Change_password($id = null) {

$this->User->id = $id;
if ($this->User->save($this->request->data)) {
$this->User->saveField('password', $this->request->data['User']['password']);
}

如果使用前保存功能,如果密码字段不为空,则自动以哈希格式保存。

【讨论】:

    【解决方案2】:

    试试这个:

     if ($this->User->updateAll(array('User.password' => "'".$this->request->data['User']['password']."'"),array('User.id' => $newsid))) {   
                    $this->Session->setFlash("Password Changed");
                  $this->redirect(array('controller' => 'admins', 'action' => 'login'));  
    
                }
    

    字符串值应该用引号括起来。

    【讨论】:

    • 是的,你解决了我的问题。缺少一个简单的单行代码。
    • 请在提供此类建议时,不要忘记提及以这种方式传递仅 100% 安全数据绝对至关重要,因为该值没有被转义,因此对 SQL 注入不安全。 Noobs 会在不知道可能的影响的情况下复制并粘贴此内容。最好按照文档中的说明告知to use the database drivers value() method
    猜你喜欢
    • 2022-12-23
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    • 1970-01-01
    相关资源
    最近更新 更多