Simple Login with CodeIgniter in PHP

CodeIgniter is an open source Web Application framework built in PHP designed to make your life as a programmer easier, while allowing you good speed for development, and also good performance when the site is up and running.

Being a Java developer for over 10 years, when I had to move to PHP I chose CodeIgniter for the following reasons:

  • Easy to install and configure (being a newbie in PHP this was crucial)
  • Clean and elegant MVC implementation
  • Uses Active Record pattern for database access
  • Overall small footprint and good performance

Usually when you are building a program, the login/logout functionality is a must we always have to go through, so this quick tutorial will focus on this functionality, taking advantage of the benefits of using CodeIgniter instead of doing it from scratch in PHP.


  • CodeIgniter framework.  By the time this tutorial was done, the latest version was 2.0.2
  • Any Apache/PHP/MySQL stack.  You can install the applications independently, or install one of those packages that have all of them bundled together.

Installing CodeIgniter

To install CodeIgniter, you only need to uncompress the Zip file you download from the site into your htdocs directory and you’re good to go.  We’ll configure the database access later.

Create the database

For this tutorial, you need a MySQL database with the following table:

Remember also to add at least one user.  We’ll add one user called bob with password supersecret.


Configure CodeIgniter

Database Access

Update the file application/config/database.php in your CodeIgniter installation with your database info:


Default Controller

We need to tell CodeIgniter to land into our login page instead of the default welcome page.  Update the file application/config/routes.php in your CodeIgniter installation with you controller’s name.  We’ll call our landing controller login.


Default Libraries

In the file application/config/autoload.php you can configure the default libraries you want to load in all your controllers.  For our case, we’ll load the database and session libraries, since we want to handle user sessions, and also the URL helper for internal link generation


Encryption Key

When you use the session library, you need to set the encryption_key in the file application/config/config.php.


The Code

Here are the actual Views, Controllers and Model we are using for the login functionality.

User Model (application/models/user.php)

 Login Controller (application/controllers/login.php)



Login View (application/views/login_view.php)


VerifyLogin Controller (application/controllers/verifylogin.php)

This controller does the actual validation of the fields and checks the credentials against the database.


Home Controller (application/controllers/home.php)

This is the private page (only authenticated users can access it).


Home Page View (application/views/home_view.php)


The code is pretty easy to follow and understand.  Also, you can download the code from here, so you can install it and test it in your location.  You’ll only need a full installation of CodeIgniter 2.0.2 and the table in your MySQL database.  If you need any help, feel free to leave us a comment or shoot us an email.

Also, this code uses a pretty basic form validation from CodeIgniter.  If you need a more complex validation process, check CodeIgniter’s Form Validation docs at their site.



  1. irul · August 18, 2013

    Its very nice script.. very helpfull for newbe like me… thankyou..

    • Gyanranjan · August 18, 2013

      Thank You,
      This is a well website for me to work with codeigniter.

  2. Gajender · August 18, 2013

    It’s really a very easy to follow code.
    Thank you for posting.
    I have just started learning code ignitor. It would be really helpfull for me if u give some links and sources from where I can get the material to practice.