匹配的Codeigniter表单验证规则(密码) [英] Codeigniter Form Validation Rule for match (password)

查看:82
本文介绍了匹配的Codeigniter表单验证规则(密码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的规则很简单,并在下面给出

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']?>">

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

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?

推荐答案

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

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.

这篇关于匹配的Codeigniter表单验证规则(密码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆