How to Create User login using codeigniter?

Today I will teach you how to create user login using codeigniter ,Let’s create login controller as below.
With this login we can create a function called as user_login(); that will load our login view. keep in mind, for good programming behavior, we will also include our constructor function, with in that call the parent constructor as well.

Once we created our login controller, we can go to the front and create our login view file. Here
we will create a very easy form, using some css styles to look superior. if needed, you can add this view file directly to your project.

READ  How to use codeigniter validations in web application.

If you observe above, the form action is set to – user_login. This is supercilious that your base_url is set as the following: “http://localhost/demos”. Notice the final slash.
So now, when we submit the login details it will navigate to user_login method. Within this method, we will call our model, and procedure our logic in the model. Let’s do so:

READ  How to use ajax post method in codeigniter form insert?

Now in the model we will create a method called validate. In this method we will query our database and get the user details and will return true or false, depending on the outcome. Also, we will use the codeigniter sessions class, to create a user-specific session. Now let’s check the method once.
If the confirmation was unsuccessful, then we need to inform the user that something is wrong.
Let’s add in a message representing the user to enter the valid username and password.
Using codeigniter session flash data we will create a message. Now we will add a parameter to our function in login controller. The evasion message will be null, sense there will be nothing to display, but if error occurs, we call the method with a message
Thanks for reading this article..
Live Demo

READ  Dropdown populate using Codeigniter and javascript

4 Comments

  1. Adi March 18, 2015
  2. SIVA SANKAR MOLKA March 24, 2015
  3. J. Moi May 19, 2015
  4. admin May 19, 2015

Leave a Reply