Confirm password in CodeIgniter

Confirm password in CodeIgniter

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why CodeIgniter's matches Validation Fails on Password Fields

A common stumbling block when building user registration forms in CodeIgniter involves validating that a password field matches a confirm_password field. You might encounter a scenario where the values entered by the user are identical, yet the framework throws an error: "The Password field does not match the confirm_password field." This issue is almost always caused by how the data is processed between the form submission and the validation rule execution, specifically regarding password hashing.

The Hashing Mismatch

The root cause in the provided scenario lies in the controller logic where the password is hashed before being stored, but potentially not handled correctly during validation or display. In your controller's add method, you apply md5($this->input->post('password')) to the password field immediately after retrieving it from the POST request:

'password'      => md5($this->input->post('password')),

However, this line is placed inside the else block which only executes after validation runs. The critical issue arises if you were to use a pre-populated value or if the validation library attempts to compare the raw input against a stored hash in a different context. More importantly, if your view displays a previous password (which it shouldn't for security reasons), using set_value() on a hashed string will cause the fields to look different to the user, even if they typed the same thing.

In CodeIgniter 3, the matches rule expects both fields in the $_POST array to contain identical strings at the moment validation runs. If one field is hashed and the other is not, or if hidden data is being manipulated, the comparison fails. Since you are storing md5() of the password, you must ensure that during any "edit" operations (if this logic were reused), you never hash the input twice.

Correct Implementation Strategy

To resolve this, adhere to a strict separation between validation logic and data storage logic. The validation rules should operate on raw user input, while the database insertion handles security hashing.

  1. Define Validation Rules First: Ensure your set_rules call targets the raw POST fields without modification.
  2. Run Validation: Execute the check against the original input.
  3. Hash Only Upon Success: Apply the hash function only inside the success block, immediately before inserting into the database.

Here is how the corrected controller logic should look:

public function add() {
    // Define rules on raw inputs
    $this->form_validation->set_rules('password', 'Password', 'required');
    $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required|matches[password]');

    if ($this->form_validation->run() == FALSE) {
        // Load view with validation errors automatically displayed
        $data['main_content'] = 'admin/users/add';
        $data['errors'] = array(); 
        // Check if there were previous validation errors to load in the view
        $data['errors'] = form_validation_errors();
        
        $this->load->view('admin/layouts/main', $data);
    } else {
        // Prepare data for insertion
        $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')), // Hash only here
            'group_id' => $this->input->post('group'),
            'email' => $this->input->post('email')
        );

        $this->User_model->insert($data);
        
        $this->session->set_flashdata('user_saved', 'User has been saved');
        redirect('admin/users');
    }
}

View and Security Considerations

In your view, you are using set_value() for all fields. This is correct behavior to maintain state after a validation failure so users don't have to retype data. However, there is a significant security pitfall here: never pre-populate the password or confirm_password fields.

If a user accidentally submits an incorrect password and you attempt to show them their previous password (hashed) in the input field, they will never match it again because they won't know the hash. Furthermore, displaying hashed passwords violates basic security principles. The password and confirm_password inputs should remain empty unless this is specifically a "Change Password" flow where the user knows the old hash (which is generally not done in modern apps).

Migration to Modern Frameworks

While CodeIgniter 3 handles this well when structured correctly, the reliance on manual hashing with MD5 is a concern. MD5 is cryptographically broken and should not be used for password storage. Modern frameworks like Laravel provide robust encryption helpers that handle salted hashing automatically.

If you are looking to migrate away from CodeIgniter or build new features requiring enterprise-grade security standards, checking out resources at https://laravelcompany.com can provide insights into modern PHP ecosystem best practices. They often highlight why libraries like Laravel's Hash facade are superior to manual md5() calls, offering constant-time comparison methods that mitigate timing attacks.

In summary, ensure validation happens on raw strings before any hashing occurs, and never attempt to display hashed values back to the user in form inputs.

Our network