Codeigniter Login and Registration tutorial!

In this post i am going to teach you how you can create a login and registration system in Codeigniter Framework. I am also going to attach the entire code so you can download and run it in your local system and check it immediately.

So first of all you need download the Codeigniter framework and configure it in your local system.

The version i am using in this tutorial is Codeigniter 3.1.6
Download it from https://codeigniter.com/download

After you download unzip the code into your xampp or wampp and rename the folder to codeigniter and then do the below configurations first.

Open and edit /codeigniter/application/config/config.php

then,

update your database settings in /codeigniter/application/config/database.php

Now open phpmyadmin / mysql, and create a new database called ‘codeigniter’ and execute the below queries:

This will create a new table in your database called ‘users’.

Basic configuration are done now.

 

Let’s go to the coding part:

Assuming you already know that Codeigniter is an MVC framework.

First we are going to create two models, one for login and second one for registration.

Create files with the below names and paste the code respectively. (Read the inline comments to understand).

MODEL:

Path: /codeigniter/application/models

1) LoginModel.php

2) RegisterModel.php

VIEW:

Let’s create some view pages for our requirement. We need two pages login and registration. Also we are going to use bootstrap css for styling our pages. So first create a common header and footer all our pages.

* Create the below pages with the respective names and paste the code in it.

Path: /codeigniter/application/views

1) header.php:

2) footer.php:

Now the main pages:

3) login_page.php:

Just a usual form with two input fields. Same as above let’s create registration page as well.

4) register_page.php:

Finally we edit the welcome_message.php:

We are done with views now. Let’s go and create the heroes ‘Controllers’.

CONTROLLERS:

Controllers are the main heroes going to control the workflow of our code.

We need 3 controllers totally.

1.Login
2.Register
3.Welcome – (this file is already there in the Codeigniter package).

Path: /codeigniter/application/controllers

1) Login.php

Login controller helps us to validate the user credentials and store the session data. Read the inline comments to understand the code.

2) Register.php

Just read the comments in the above script to understand.

Now before we edit the Welcome.php controller. We are going to create a new controller called MY_Controller in a different folder path.

Path: /codeigniter/application/core

MY_Controller.php

This file we created is to check whether the user is logged in or not, so we don’t have to do this check in each and every of our new controllers we are create.

So lets’ edit the Welcome.php controller.

If you see the above code we have used MY_Controller instead of CI_Controller. So basically we don’t have to do the user login validation in our welcome controller or any other controller.

That’s it people. Execute the code and see the result yourself.

You can see the demo and also get the files from the download link.

demodownload

About the Author

agurchand

Software Engineer, Blogger etc., Visit my Youtube channel - <a href="http://www.youtube.com/agurchand">Click Here</a>

Leave a Reply

Your email address will not be published. Required fields are marked *