How To Make A Login Page In Codeigniter

How To Articles

Creating a login page is an essential part of many web applications. In this article, I will guide you through the process of making a login page in CodeIgniter, a popular PHP framework. As a web developer who has worked extensively with CodeIgniter, I have found it to be a powerful and user-friendly framework that simplifies the development process.

Setting up CodeIgniter

If you haven’t already, start by downloading and installing CodeIgniter from the official website. Once the installation is complete, you can begin creating your login page.

Creating the Controller

In CodeIgniter, the logic of our application resides in controllers. To create a new controller, navigate to the application/controllers directory and create a new file named Login.php. Open the file in your favorite text editor and add the following code:

<?php
class Login extends CI_Controller {
    public function index() {
        $this->load->view('login_view');
    }
}

In this code, we are creating a new controller named “Login” and defining an index method. Inside the index method, we load the login_view view, which we will create next.

Creating the View

In CodeIgniter, views are used to display the user interface. To create the login view, navigate to the application/views directory and create a new file named login_view.php. Open the file and add the following code:

<html>
  <head>
    <title>Login</title>
  </head>
  <body>
    <h2>Login</h2>
    <form action="<?php echo base_url('login/validate'); ?>" method="post">
      <label for="username">Username:</label>
      <input type="text" name="username" required>
      <label for="password">Password:</label>
      <input type="password" name="password" required>
      <input type="submit" value="Login">
    </form>
  </body>
</html>

In this code, we have created a basic login form. The form’s action attribute is set to <?php echo base_url('login/validate'); ?>, which will be the URL that the form data is submitted to for validation. We have also added input fields for the username and password, as well as a submit button.

Creating the Validation

Now that we have our login form, we need to create the validation logic. In the Login.php controller file, add the following code:

<?php
class Login extends CI_Controller {
    public function index() {
        $this->load->view('login_view');
    }

    public function validate() {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        // Add your validation logic here
    }
}

In the validate method, we are retrieving the username and password submitted through the form using the input->post method. You can add your validation logic here, such as checking if the user credentials exist in your database or any other custom validation you require.

Conclusion

Congratulations! You have successfully created a login page in CodeIgniter. In this article, we explored the process of setting up CodeIgniter, creating the controller and view for the login page, and validating the login credentials. CodeIgniter provides a solid foundation for building secure and efficient web applications. With some additional customization and security measures, you can further enhance the functionality of your login page. Happy coding!