CodeIgniter 3: The Login Page Part 1

So now that we have a basic understanding of what the different parts of CodeIgniter are, we can get down to writing our first controller. The login controller. As we don’t want just anyone being able to access our site.

This is what we will be aiming for:


The first thing we need to do is delete the default controller called “welcome”. This can be found in the application/controllers directory. Once that’s deleted, open up a new file, in the same directory, and call it Login.php.
A quick aside about controllers. All controllers must define their class (as you will see below). If no function within the controller is called, it defaults to the index() function.

So first off copy the following code into the controller

<?php defined('BASEPATH') or exit('No direct script access allowed');
class Login extends CI_Controller {     
    public function __construct() {
    public function index($error = "") {
        $data["error"] = $error;
        $this->view->load("login", $data);
    public functio check() {
        $this->input->post("username", true);
        $this->input->post("password", true);
        $valid = $this->login_model->validate($username, $password);
        if ($valid === true) {
            redirect("/home/", "refresh");
        } else {

The first function, or the construct function, is called at start, regardless of the function being called. This is the construct function. Any libraries or helpers required in the module should be loaded here as well as any other code that you might need across the controller. In my code I have used it to load the form helper (which I'll go into more later) and the login model that we will create shortly. Always make sure you call the parent classes, CI_Controller's construct function too!

The second function, or index, is called by default if no method is supplied by the URL. In this function any existing session is destroyed and the login page is loaded. The functions argument defaults to null as this will hold any errors that come from the user attempting to login. This argument is assigned to a key in an array and loaded with the page.

The third function, or the check function, is called when the user attempts a login. The username and password are retreived from the POST variable and then passed to the model where they can be checked against a database of users. Depending on the return value of the check function in the model, the controller will either redirect the user to the home page or send them back to the login page with the error message.

Now that we have our controller, we need to create the model, the code that actually checks a users identity and setup the database and various libraries that are needed.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s