Create User login & check Login Status with codeigniter

Create User login with CodeIgniter

At some point you might need to create a user login for your project while using the codeigniter framework. For those of you who are just getting started with this, you will see within this next post that it is very simple to create.

Let’s cut right to the chase:

Create login controller

The first thing that needs to be done is the login controller for your project. Within this login, we will create an index function that will load our login view. Remember, for good programming habits, we will also include our construct, and call the parent construct as well.

Filename: login.php

Once our logion controller is created, we can go ahead and create our login_view file. I will create a very simple form, without any style. That way, if needed, you can add the style that you like.

Filename: login_view.php

If you notice above, the form action is set to – base_url()login/process. This is assuming that your base_url is set as the following: “http://yourdomain.com/”. Notice the final slash. So now we need to create a method in our controller called process. Within this method, we will call our model, and process our logic in the model. Let’s do so:

Now that we have that method, we can create our login_model file. This file will have a method called validate. In this method we will query our database, looking for a user, and will return true or false, depending on the outcome. Also, we will use the codeigniter sessions class, to create a user-specific session. Let’s take a look at how its done.

Filename: login_model.php

We are almost finished with this login script. If we have that the validation was unsuccessful, then we need to notify the user that something is wrong. Let’s add in a message indicating something went wrong. We will add a parameter to our index function in login controller. The default message will be null, meaning there will be nothing to display, but if an error occurs, we call the method with a message. Let’s see how we go about this matter:

Filename: login.php

Now let’s display that message in our login_view:

Filename: login_view.php

Lastly, we need to create the members only page and controller. This controller will verify if the user is logged in, and if not, then redirect to the login page. We will create the validation within the construct, since each time the user access this ‘home’ page, this construct will run. If we were to only write it in the index function, then only the index would get validated, and not all other functions.

Add Logout

For everything that get’s logged in, there should be a way to log out. It very simple, and we will add this logic to our home controller. Also, we will add a link in the members area to logout.

There you have it folks, a simple logic for logging in and out a user from a website. This can obviously be enhanced to your likings as I have made this post very broad to show how quickly this can be done.

Recommendations

Something to remember is to load your session library in the autoload file. Also, you need to set an encryption key, even if you are not planning on using it, codeigniter requires you to create one.

Load the sessions library like this:

Filename: autoload.php – Can be found at: path/to/ci/folder/application/config/autoload.php

To set up the encryption key, go to the file config.php

Filename: config.php – Can be found at: path/to/ci/folder/application/config/config.php

With this configured, you are ready to execute the script.

Leave a Reply

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