确认CodeIgniter中的密码

63lcw9qa  于 2023-01-18  发布在  其他
关注(0)|答案(5)|浏览(163)

我正在用CodeIgniter 3.1.3创建一个博客,我在管理中有一个添加新用户的功能。当我尝试添加新用户时,我收到以下错误:
密码字段与confirm_password字段不匹配。
尽管在密码和CONFROM密码字段中给定的密码是相同的。
我想不出问题的原因。

这是我的模型:

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

"这是我的控制器"

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

以下是我的观点:

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

c0vxltue1#

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

把你的表单验证改为这样,记住在config/autoload.php文件中自动加载表单验证,比如$autoload['libraries'] = array('form_validation');。如果它不起作用,请在下面评论。

20jt8wwn

20jt8wwn2#

下面是一个干净简单的方法来实现这一点。
下面是我的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');    
    }     
}

现在进行表单验证:
1.在config文件夹中创建form_validation.php文件。
1.然后在autoload.php文件中写入此行。
$autoload['libraries'] = array('form_validation');
下面是我的代码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]'
                                    ]
                            ]
];

?>
lvjbypge

lvjbypge3#

配置/自动加载. php:

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

查看:

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

控制器:

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

eoigrqb64#

您必须使用两次设置规则才能进行双密码确认

$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

ebdffaop

ebdffaop5#

在中创建表单验证文件

  • 应用程序/配置/表单验证.php*

编写下面的代码

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

控制器代码

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

相关问题