简体   繁体   English

在 CodeIgniter 中确认密码

[英]Confirm password in CodeIgniter

I'm building a blog with CodeIgniter 3.1.3 and I have a feature in the admin to add new users.我正在使用 CodeIgniter 3.1.3 构建一个博客,并且我在管理员中有一个添加新用户的功能。 When I try to add a new user I get the following error:当我尝试添加新用户时,出现以下错误:

The Password field does not match the confirm_password field.密码字段与confirm_password 字段不匹配。

although the given password in password and confrom password field is the same.尽管 password 和 confrom password 字段中给定的密码是相同的。

I can't figure out what causes the problem.我无法弄清楚是什么导致了问题。

Here is my model:这是我的模型:

public function insert($data){
    $this->db->insert('users', $data);
    return true;
}

Here is my controller这是我的控制器

public function add(){
        //Validation Rules
        $this->form_validation->set_rules('first_name','First Name','trim|required');
        $this->form_validation->set_rules('last_name','Last Name','trim|required');
        $this->form_validation->set_rules('email','Email','trim|required|valid_email');
        $this->form_validation->set_rules('username','Username','trim|required|min_length[3]');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[confirm_password]');

        $data['groups'] = $this->User_model->get_groups();

        if($this->form_validation->run() == FALSE){
            //Views
            $data['main_content'] = 'admin/users/add';
            $this->load->view('admin/layouts/main', $data);
        } else {
            //Create Data Array
            $data = array(
                'first_name'    => $this->input->post('first_name'),
                'last_name'     => $this->input->post('last_name'),
                'username'      => $this->input->post('username'),
                'password'      => md5($this->input->post('password')),
                'group_id'      => $this->input->post('group'),
                'email'         => $this->input->post('email')
            );

            //Table Insert
            $this->User_model->insert($data);

            //Create Message
            $this->session->set_flashdata('user_saved', 'User has been saved');

            //Redirect to pages
            redirect('admin/users');
        }

Here is my view:这是我的观点:

<div class="row">
                        <div class="col-lg-12">
                            <div class="form-group">
                                <label>First Name</label>
                                <input class="form-control" type="text" name="first_name" value="<?php echo set_value('first_name'); ?>" placeholder="Enter First Name" />
                            </div>
                            <div class="form-group">
                                <label>Last Name</label>
                                <input class="form-control" type="text" name="last_name" value="<?php echo set_value('last_name'); ?>" placeholder="Enter Last Name" />
                            </div>  
                            <div class="form-group">
                                <label>Email Address</label>
                                <input class="form-control" type="email" name="email" value="<?php echo set_value('email'); ?>" placeholder="Enter Email" />
                            </div>  
                            <div class="form-group">
                                <label>Username</label>
                                <input class="form-control" type="text" name="username" value="<?php echo set_value('username'); ?>" placeholder="Enter Username" />
                            </div>  
                            <div class="form-group">
                                <label>Password</label>
                                <input class="form-control" type="password" name="password" value="<?php echo set_value('password'); ?>" placeholder="Enter Password" />
                            </div>
                            <div class="form-group">
                                <label>Confirm Password</label>
                                <input class="form-control" type="password" name="confirm_password" value="<?php echo set_value('confirm_password'); ?>" placeholder="Confirm Password" />
                            </div>
                            <div class="form-group">
                                <label>User Group</label>
                                <select name="group" class="form-control">
                                     <?php foreach($groups as $group) : ?>
                                    <option value="<?php echo $group->id; ?>"><?php echo $group->name; ?></option>
                                 <?php endforeach; ?>   
                                </select>
                            </div>  

                    </div><!-- /.row -->
$this->form_validation->set_rules('first_name','First Name','trim|required');
$this->form_validation->set_rules('last_name','Last Name','trim|required');
$this->form_validation->set_rules('email','Email','trim|required|valid_email');
$this->form_validation->set_rules('username','Username','trim|required|min_length[3]');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required|matches[password]');

Change your form validation to this and remember to autoload form validation in config/autoload.php file like $autoload['libraries'] = array('form_validation');将您的表单验证更改为此并记住在 config/autoload.php 文件中自动加载表单验证,如$autoload['libraries'] = array('form_validation'); . . If it doen't work please comment below.如果它不起作用,请在下面发表评论。

config/autoload.php:配置/自动加载.php:

$autoload['libraries'] = array('form_validation');

View:看法:

<?php echo form_label('Password:'); ?>
<?php echo form_input(array('placeholder'=>'Enter your password', 'type'=>'password', 'name'=>'password')); ?>

<?php echo form_label('Confirm password:'); ?>
<?php echo form_input(array('placeholder'=>'Confirm your password', 'type'=>'password', 'name'=>'confirm_password')); ?>

Controller:控制器:

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

Here is the clean and simple way to achieve that.这是实现这一目标的干净简单的方法。

Here is my controller code:这是我的controller代码:

public function add_user(){
    $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');    
    if ($this->form_validation->run('register_user_rules') == FALSE){
        $this->load->template('user/register');     
    } else {
        $post = $this->input->post();
        unset($post['pass_confirm']);
        unset($post['submit']);
        if($this->UserModel->addUser($post)){
            $this->session->set_flashdata('feedback', 'Registration Succesfull! Please Sign in to Continue');
            $this->session->set_flashdata('feedback_class', 'alert-success');
        } else {
            $this->session->set_flashdata('feedback', 'Sorry! Please try again.');
            $this->session->set_flashdata('feedback_class', 'alert-danger');
        }
        return redirect('register');    
    }     
}

Now for Form validation:现在进行表单验证:

  1. Create a form_validation.php file in config folder.config文件夹中创建一个form_validation.php文件。

  2. Then inside autoload.php file write this line.然后在autoload.php文件中写下这一行。

    $autoload['libraries'] = array('form_validation');

Here is my code of form_validation.php file.这是我的form_validation.php文件代码。

<?php

$config = [
        'register_user_rules' => [
                                    [
                                        'field' => 'fullname',
                                        'label' => 'Full Name',
                                        'rules' => 'trim|required|max_length[20]'
                                    ],
                                    [
                                        'field' => 'email',
                                        'label' => 'Email Address',
                                        'rules' => 'trim|required|valid_email'

                                    ],
                                    [
                                        'field' => 'phone',
                                        'label' => 'Phone Number',
                                        'rules' => 'trim|required|numeric|max_length[10]|regex_match[/^[0-9]{10}$/]'

                                    ],  
                                    [
                                        'field' => 'password',
                                        'label' => 'Password',
                                        'rules' => 'trim|required'
                                    ],
                                    [
                                        'field' => 'pass_confirm',
                                        'label' => 'Confirm Password',
                                        'rules' => 'trim|required|matches[password]'
                                    ]
                            ]
];

?>

You must use set rules two times for the double password Confirmation您必须使用两次设置规则进行双重密码确认

$this->form_validation->set_rules('username','Username','trim|required|min_length[3]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required|matches[confirm_password]'); 

http://prntscr.com/kl4mfu http://prntscr.com/kl4mfu
http://prntscr.com/kl4m7u http://prntscr.com/kl4m7u
http://prntscr.com/kl4lvm http://prntscr.com/kl4lvm

Create the form validation file in在中创建表单验证文件

application/config/form_validation.php应用程序/配置/form_validation.php

write the below code编写下面的代码

<?php $config =   [
'add_user'  =>  [

   [
        'field'     =>  'first_name',
        'label'     =>  'First Name',
        'rules'     =>  'trim|required'
    ],
    [
        'field'     =>  'last_name',
        'label'     =>  'Last Name',
        'rules'     =>  'trim|required'
    ],
    [
        'field'     =>  'email',
        'label'     =>  'Email Address',
        'rules'     =>  'required|valid_email'
    ],
    [
        'field'     =>  'username',
        'label'     =>  'Username',
        'rules'     =>  'trim|required|min_length[3]'
    ],
    [
        'field'     =>  'password',
        'label'     =>  'Password',
        'rules'     =>  'required|matches[confirm_password]'
    ]
]];

Controller code控制器代码

function add(){
$this->load->helper('form');

if($this->form_validation->run('add_user')){
    
    $post   =   $this->input->post();

    $_data  =   array(
        'first_name'       =>   $post['first_name'],
        'last_name'        =>   $post['last_name'],
        'username'         =>   $post['username'],
        'password'         =>   md5($post['password']);
        'group_id'         =>   $post['group'],
        'email'            =>   $post['email']  
    );

    // insert into table
    $_insert = $this->User_model->insert($_data);
    
    if($_insert){
        // if data is saved show the success message.
        $this->session->set_flashdata('user','User has been saved');
    } else {
        // if failed to save show the failed message.
        $this->session->set_flashdata('user','Failed to save the user data');
    }
    // Redirect to users page
    return redirect('admin/users');
} else {
    $data['main_content']   =   'admin/users/add';
    $this->load->view('admin/layouts/main',$data);
}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM