Codeigniter: How can I encrypt password before submitting the form to the controller?

Codeigniter: How can I encrypt password before submitting the form to the controller?

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

Stefan Bogdanescu

Founder & Senior Architect

Codeigniter: How can I encrypt password before submitting the form to the controller?

When a user submits a login form, the data travels from their browser to your server via an HTTP request. In the example provided, the form sends the plaintext password pass_word directly to the controller's login() method. If the controller simply echoes this value back—such as with $this->input->post('pass_word')—the result is immediate and obvious: the raw password appears in the output.

This behavior highlights two critical security flaws. First, displaying the password in the response confirms that the application does not treat authentication data as sensitive until it reaches the database or a hash comparison function. Second, transmitting passwords in plaintext over the network exposes them to interception. Tools like Wireshark can capture unencrypted HTTP traffic, allowing an attacker on the same network segment to read credentials directly from packet captures.

Why Encryption in the View is Not the Solution

The core question asks if we can encrypt the password in the view (HTML) before sending it to the controller. The short answer is no, and attempting to do so introduces significant architectural pitfalls.

Client-side encryption is fundamentally insecure because the client environment is not trusted. If you use JavaScript or a library to hash the password before submission, the source code of that script becomes visible to any user inspecting their browser's developer tools. A determined attacker can easily reverse-engineer the hashing algorithm and key used in the view, rendering your "encryption" useless against someone who simply modifies the request payload before it even leaves their machine.

Furthermore, CodeIgniter's form_validation library, which is commonly paired with views containing forms, operates server-side. Relying on client-side logic for security violates the principle of least trust. Security must be enforced on the server, where you control the environment and can verify that the data received matches what was originally sent without relying on hidden scripts or keys exposed to the user.

The Proper Approach: HTTPS and Server-Side Hashing

To address the network monitoring concern, the primary defense is enforcing HTTPS (SSL/TLS). When a site uses HTTPS, all traffic between the client and server is encrypted at the transport layer. Wireshark will then show only ciphertext, making it impossible to read the password in transit without the session keys. If your form currently uses http://, you must configure your web server (Apache/Nginx) or hosting provider to redirect all traffic to https://.

Regarding the storage and verification of passwords, you should never store plaintext passwords in the database, even if you hash them on submission. The standard industry practice is to hash the password once it reaches the controller, before storing it, and then compare hashes during login.

In CodeIgniter, the hash() function provides a simple way to generate SHA1 hashes, though this method is largely considered deprecated for new development due to its speed and lack of salt management. Modern applications should utilize libraries like phpseclib or native PHP functions that support Argon2id or PBKDF2, which are resistant to brute-force attacks.

A robust implementation in the controller would look like this:

public function login() {
    $password = $this->input->post('pass_word');
    
    // In a real application, you should validate input length and existence first
    
    // Use a strong hashing algorithm (e.g., from phpseclib or native PHP)
    // Example using native password_hash for modern security standards
    $hashed = password_hash($password, PASSWORD_DEFAULT);
    
    // Store the hash in your database, not the plain text
    // $this->db->insert('users', ['password' => $hashed]);
    
    // For login verification, use password_verify instead of direct string comparison
    if (password_verify($password, $this->db->row('user_password'))) {
        // Grant access
    }
}

Conclusion on Security Architecture

The takeaway is that you cannot and should not attempt to encrypt passwords in the view. The security model relies entirely on:

  1. Transport Security: Enforcing HTTPS via server configuration to prevent network interception.
  2. Server-Side Processing: Generating cryptographic hashes within the controller logic, not the browser.
  3. Secure Storage: Storing only the salted hash in the database, never the original plaintext.

By shifting the hashing logic to the server and securing the transport layer, you eliminate both the network monitoring risk and the risk of accidental plaintext exposure in logs or responses.

Our network