Complete User Authentication System in CodeIgniter - 9 Steps

Complete User Authentication System in CodeIgniter – 9 Steps

Creating User Authentication System in CodeIgniter that is User Registration and Login features, you will learn about how we can create these features in CodeIgniter.

First, we will start off with creating a database table, then set up controllers views and routes. After that I’ll add plain HTML forms to register view and login view, then I’ll convert these HTML forms into CodeIgniter forms.

Then we will test by submitting the forms for that we need form submission controllers and routes. Then we will add form validations to register and login forms for checking after form submissions using CodeIgniter’s form validation library.

Then we will work on registration functionality that is inserting the submitted user information into the database after that login functionality that is checking user exists with the submitted username and password. If the values exist in our database then we will create the session and redirect the user to the member’s area.

First of all, we will start off going through the HTML files that is we need registration page and login page. We will what are the fields we will be using in our User Authentication Application.

Then we will create the database table and connect to it.

Are You Excited to Build User Authentication System in CodeIgniter?

I've good News for you! Created a Video Course with 1.5 Hours of Content...

1. Register & Login HTML files

These are the HTML files of Register and Login pages, in register.html

Below is the code of register.html we will use this code in register view while after creating the views. It’s a simple bootstrap form with 4 input fields. First one is the username and the second field is an email address, third fourth fields are password and Password Again for comparing password. A submit button and a login anchor link.

This is simple login page with two fields that is username input field and the password input field. Then after that submit button with for login and an anchor link to registration page.

2. Creating Database table

Here I’m creating a simple table with the name of users inside the cilogin database. Here I’m creating a users table with 8 columns. The first column is the id that is an integer type with length 11 primary key and auto increment.

Second Column is the username that is of type varchar length 255 and unique, email is of type varchar with length 255. The password is of type varchar with length 255, active is boolean to store true are false default value is false. active_key and reset_key column are for storing activation keys while activating user account on registration and reset key for resetting the password. I’m not covering these two features for now.

User this below SQL code to create users table.

3. Setting Up CodeIgniter & Connecting to Database

For setting up CodeIgniter, you should download the latest version of CodeIgniter. As of writing this article I’m using CodeIgniter version of 3.1.6.

After downloading extract it in a separate directory on your server. Access this directory in the browser you should see a screen like this.

CodeIgniter Setup Screen

CodeIgniter Setup Screen

If you are able to see the above screen, you have successfully done the CodeIgniter setup. Next, we need to configure the database for that go to application/config/database.php, you should see code like this, update these key values that are hostname username database and password.

Also, add database and session library in autoload.php file

Also, we should load URL helper and form helper.

After making the changes to database configuration I’m inserting a new user record with simple SQL query, Add this code to the welcome controller which you can find in controllers/welcome.php. After adding this code reload the page again so that record will be inserted.

4. Setting Up Controllers, Views, Routes

In this step, I’m going to create controllers views and routes for associating them.

First of all, I’ll create the User Controller in controllers directory. I’m saving it as User.php

If you load these urls “user/index”, “user/login”, “user/register”, “user/logout”. You should see the messages.

Here what I want to do is I want to make these URLs as “user/login” as “login” and “user/register” as “register”. We can do this with CodeIgniter Routes with routes.php file located in application/config/routes.php

After that, I’m creating views for registration page and login page in application/views/user with the name of register.php and login.php. Add the above HTML code.

Also for reusability, we need template files header.php and footer.php

Add this code to header.php

Add this code to footer.php

Are You Excited to Build User Authentication System in CodeIgniter?

I've good News for you! Created a Video Course with 1.5 Hours of Content...

6. Making HTML forms as CodeIgniter Forms

Instead of Using HTML forms, we can use the CodeIgniter form library previously I’ve loaded form helper.

You can use this codd in register.php file located in applicaion/views/user/register.php

And add this code login.php file located in application/views/user/login.php

Load this views in User Controller inside login method and register method.

If you access these pages login or register URLs now you should see the CodeIgniter forms for registration and login functionalities.

7. Adding Form Validation to Register & Login

For adding form validations in CodeIgniter, I’m using CodeIgniter’s form validation library.

First of all, I’m adding form validations to the registration form that is in register method. Here I’m checking username is required and unique from users table username column. Checking that email is required and adding validations for the password is required and passwordagain field matches with the password field.

These are the rules and code for adding rules is here.

If it’s not true I’ll display the error messages with validations errors message by adding few bootstrap classes with danger to show it in red color

Next, I’ll add form validations to the login form. For login form, I’m submitting the information to the login_post controller we don’t have this controller. I’ll create this controller with the validations using form validation library.

If there are errors I’m loading the login method to show the form with errors. Else I’ll add the login logic which we will see it in next steps.

Add this line of in login.php file located at views to display the error messages with bootstrap danger message.

8. User Registration Functionality with CodeIgniter

For proceeding with registration functionality we should create the model with the name of user_model that means it’s related to user controller.

I’m creating a model with the insert_user method to insert user information into the database. Here in this insert_user method I’m passing the submitted information and using this information in insert SQL query.

After adding the above code, I’ll load this model inside register method in User controller. I’m creating an alias of this user_model as auth, from here onwards while loading model methods we can use auth.

Add this below code in register-update, this is the view I’m loading after checking the validations.

After inserting the user information, I’m setting flashmessage as “User Registered Successfully” and redirecting the user to register route.

To display that message in register controller, I’m using this line of code

After adding all the above code, you can try submitting the form to insert the record. That user information should be inserted.

9. User Login Functionality with CodeIgniter

After user registration user should be able to log in and log out, here in this step we will see how we can add that functionality with CodeIgniter

Here, I’m creating a model method inside user_model as login_user for checking the user exists in our database or not?

It’s just simple select query to check the username and password matches in the database. If it matches any number of rows equals to one then I’m returning true or false out of the model method.

We should add this method inside login_post controller method. I’m using the same code as above register method.

After checking the validations if there are no errors, I’m executing login_user model method. If it returns true I’m creating a session and redirect the user to the member’s area that in index controller in users.

Otherwise, I’m setting an error message and loading login controller method again to display error messages in the login page.

Finally, we should add the logout functionality add this code in user controller. Previously I’ve created logout controller method, paste it in there

Complete Code of all the files

If you have any problem arraning above pieces of code, you can use this complete code.


You have successfully built the user authentication system in CodeIgniter with registration and login functionality.

If you have any doubts let me know through the comment form below.

https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1822 ZP9ldwJ5im9n-M97X7Ix2RSSs1aqvwaDxj9mDvu80F2_sBE0NjMBXJiuzTEpLtNn 3384e45af215f3c2f2105003203eef6f3ee8dc2d&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1836 MTfrX5pOcY1sSR2xwBT1IggtsX45qWycHQZsvm-Zl9u0spQKisC31KAkyVmqpzQ7 4dec733650c2bf31ed8879df55405343f6bdf7e9&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 4 comments

The script above does not seem to have user account activation by email?
Am I wrong? Will you add activation if it does not have it yet?

Best regards,
Razvan Zamfir

    Vivek Vengala

    Hi Razvan, Thank you for raising the point. I’ll surely add this feature but it will take some time…


      Please let me know as soon as you do. Thank you!

        Vivek Vengala

        Join Our VIP Facebook Group to get future updates…


Leave a Reply: