codeigniter
  1. codeigniter-login-form

Login Form in CodeIgniter with Database

A login form is a basic feature that is required in many types of web applications to provide secure access to the application. In this tutorial, we will learn how to create a login form in CodeIgniter using a MySQL Database.

Syntax

The syntax for the Login Form in CodeIgniter with Database can be broken down into several parts:

  1. Form Creation: Create a login form that contains the input fields for the username and password.

  2. Form Submission: When the user submits the login form, the credentials are sent to the server for validation.

  3. Authentication: The credentials are authenticated by verifying them with the data stored in the database.

  4. Session Management: Once authenticated, a session is created for the user so that they can access protected content.

Example

Consider the following login form in CodeIgniter:

<!DOCTYPE html>
<html>
<head>
    <title>Login Form</title>
</head>
<body>
    <h2>User Login</h2>
    <form method="post" action="<?php echo site_url('login/process'); ?>">
        <label>Username:</label>
        <input type="text" name="username">
        <label>Password:</label>
        <input type="password" name="password">
        <br><br>
        <input type="submit" value="Login">
    </form>
</body>
</html>

This form contains two input fields for the username and password and a submit button. The form's action attribute is set to the URL where the credentials will be processed for validation.

The process function in the login controller is responsible for processing the form data and validating the credentials. The code for the process function is as follows:

public function process() {
    $this->load->model('login_model');
    $result = $this->login_model->validate();
    if(!$result){
        $this->index();
    }else{
        redirect('home');
    }
}

The validate function in the $login_model is responsible for querying the database and validating the user's login credentials. The code for the validate function is as follows:

public function validate(){
    $this->db->where('username',$this->input->post('username'));
    $this->db->where('password',$this->input->post('password'));
    $query = $this->db->get('users');
    if($query->num_rows()==1) {
        return true;
    }
    else {
        return false;
    }
}

The validate function queries the users table in the database to verify the user's credentials. If the user exists in the database, the function returns true and the user is redirected to the home page, otherwise, the function returns false and the user is redirected back to the login page.

Explanation

In the example above, we created a simple login form that collects the user's credential inputs and uses them to query the database for verification. The authentication process is handled by the validate function in the login_model.

If the user's credentials are valid, they are redirected to the home page, otherwise, they are redirected back to the login page.

Use

The login form in CodeIgniter can be customized and integrated into various types of web applications to provide secure access to the application.

Important Points

  • The login form should be designed to be simple, clean, and easy to use.
  • Passwords should be encrypted to prevent unauthorized access to user data.
  • The user's credentials should be validated against the data stored in the database.

Summary

In this tutorial, we learned how to create a login form in CodeIgniter with a MySQL database. The login form is a basic feature required in many types of web applications to provide secure access to the application. The authentication process was performed by verifying the credentials with the data stored in the database using the validate function in the login_model. Once authenticated, a session is created for the user so that they can access protected content.

Published on: