【问题标题】:Codeigniter Form Validation Rule for match (password)匹配的 Codeigniter 表单验证规则(密码)
【发布时间】:2015-08-30 13:15:44
【问题描述】:

我正在尝试在我的控制器中编写表单验证规则以提交更改密码表单,我也在其中检查旧密码。我从 db 获取旧密码(当前)并将其放在隐藏的输入字段中。

我的规则很简单,如下所示

         $config=array(
            array(
                'field'   => 'old_password',
                'label'   => 'oldpass',
                'rules'   => 'trim|required'
            ),
            array(
                'field'   => 'conf_password',
                'label'   => 'connewpass',
                'rules'   => 'trim|required|matches[password]'
            ),
            array(
                'field'   => 'password',
                'label'   => 'newpass',
                'rules'   => 'trim|required'
            )

我在表单中保存当前密码的隐藏输入字段就像

<input type="hidden" name="old_pass" value="<?php echo $user['password']?>">

我知道规则中的匹配(字段名称)适用于匹配两个字段值,但我卡住的地方是来自 db 的密码是 md5 加密的。如何加密来自表单的密码并与规则中的旧密码字段匹配?

【问题讨论】:

    标签: php forms codeigniter validation


    【解决方案1】:

    无需将旧密码哈希放在隐藏字段中。它甚至不安全。 您可以为自己的自定义验证创建回调函数。请注意我在以下代码中所做的评论。

    $config=array(
                array(
                    'field'   => 'old_password',
                    'label'   => 'oldpass',
                    'rules'   => 'trim|required|callback_oldpassword_check' // Note: Notice added callback verifier.
                ),
                array(
                    'field'   => 'conf_password',
                    'label'   => 'connewpass',
                    'rules'   => 'trim|required|matches[password]'
                ),
                array(
                    'field'   => 'password',
                    'label'   => 'newpass',
                    'rules'   => 'trim|required'
                )
    

    在您的控制器中创建如下方法

    public function oldpassword_check($old_password){
       $old_password_hash = md5($old_password);
       $old_password_db_hash = $this->yourmodel->fetchPasswordHashFromDB();
    
       if($old_password_hash != $old_password_db_hash)
       {
          $this->form_validation->set_message('oldpassword_check', 'Old password not match');
          return FALSE;
       } 
       return TRUE;
    }
    

    更多回调验证详情请访问here

    我还没有验证上面的代码。但希望你能找到解决问题的方法。

    【讨论】:

    • 优秀。我真的和我的同事讨论过同样的事情,这正是我的前辈建议的。谢谢
    【解决方案2】:

    另一种方法:

    if (!$this - > checkValidLogin($username, $old_password)) {
      $this - > form_validation - > set_rules('password', 'Password', [
        [
          'old_password',
          function($value) {
            return false;
          }
        ]
      ]);
      $this - > form_validation - > set_message('old_password', 'Old password doesn\'t match.');
    }
    

    【讨论】:

      【解决方案3】:

      请像这样使用,如果您使用的是表单验证库,它对我有用。

      $this->form_validation->set_rules('password', 'Password', 'required');
      $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required|matches[password]');
      

      谢谢

      编辑:代码格式

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-05
        • 2015-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-22
        相关资源
        最近更新 更多