php 在 CodeIgniter 中确认密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41640446/
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-26 02:25:56  来源:igfitidea点击:

Confirm password in CodeIgniter

phpcodeigniter

提问by PapT

I'm building a blog with CodeIgniter 3.1.3 and I have a feature in the admin to add new users. When I try to add a new user I get the following error:

我正在使用 CodeIgniter 3.1.3 构建一个博客,并且我在管理员中有一个添加新用户的功能。当我尝试添加新用户时,出现以下错误:

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

回答by Geordy James

$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');. If it doen't work please comment below.

将您的表单验证更改为此并记住在 config/autoload.php 文件中自动加载表单验证,例如$autoload['libraries'] = array('form_validation');. 如果它不起作用,请在下面发表评论。

回答by Shubham Agrawal

Here is the clean and simple way to achieve that.

这是实现这一目标的干净简单的方法。

Here is my controllercode:

这是我的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.phpfile in configfolder.

  2. Then inside autoload.phpfile write this line.

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

  1. form_validation.phpconfig文件夹中创建一个文件。

  2. 然后在autoload.php文件里面写这一行。

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

Here is my code of form_validation.phpfile.

这是我的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]'
                                    ]
                            ]
];

?>

回答by Whole Hat

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]');

回答by Imran Khan

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/kl4m7u
http://prntscr.com/kl4lvm

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