php 匹配(密码)的 Codeigniter 表单验证规则

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30838458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:06:09  来源:igfitidea点击:

Codeigniter Form Validation Rule for match (password)

phpformscodeignitervalidation

提问by Malik Mudassar

I am trying to write Form validation rules in my Controller to submit Change Password form in which I am checking the old password too. I am getting the old password(current) from db and placing it in a hidden input field.

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

My Rules are simple and are given below

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

         $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'
            )

My hidden input field in the form to save current password is like

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

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

I know that matches(field name) in rules work for matching two field values but Where I am stuck is that the password coming from db is md5 encrypted. How can I encrypt the password coming from form and match with old pass field in the rule?

我知道规则中的匹配(字段名称)可用于匹配两个字段值,但我遇到的问题是来自 db 的密码是 md5 加密的。如何加密来自表单的密码并与规则中的旧密码字段匹配?

回答by jagad89

There is no need of putting old password hash in hidden field. it's not even safe. you can create callback function for your own custom validation. Notice the comment i have did in following code.

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

$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'
            )

In side your controller create a method as below

在您的控制器旁边创建一个方法,如下所示

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;
}

for more details of callback verification visit here

有关回调验证的更多详细信息,请访问此处

I have not verified above code. But hope you get the way to solve your problem.

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

回答by Vishal P Gothi

Please use like this, if you are using form validation library, it is working for me.

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

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

Thank You

谢谢你

Edit: Code formatting

编辑:代码格式

回答by Adriano Gon?alves

Another approach:

另一种方法:

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