March 9, 2021
Hot Topics:

CodeIgnitor: Create a Login Process, Page 2

  • By Sachin Khosla
  • Send Email »
  • More Articles »

Create a Login Process

Next, we should provide a way to log in these registered users. The process is going to be the same. We create the View, then Model and finally do the processing inside the Controller.

In the model, after sanitizing the user input (as stated above), we can query the database using the following method:

 function validate_user() { $this->db->select('*'); $this->db->limit(1); $this->db->where('user_email',$user_login); $this->db->where('user_pass', md5($password)); $this->db->from('ci_users'); if($query->num_rows == 1) { $data = $query->row_array(); if($data['user_login'] == $user_login) return $data; else return false; } } 

The above method is simple to understand. I have split the query to make it clearer. If the number of rows is 1, then we validate user, otherwise we do not. We return the complete user information to the controller where we are going to create session information. Let's see how:

 if(is_array($user) &z& !empty($user)) { $user_data = array('is_logged_in' => 1,'user_email' => $user['user_email']); 
//set the session information
//write code here to show the success message
redirect(site_url('dashboard')); exit(); }

I have redirected data to the dashboard, where we are going to show user-related information. Only valid users are allowed to enter; the rest all are redirected to the login page.

Create a Logout Process

Finally, we need a way to log out users. We can destroy the current session by calling the sess_destroy() method of the built-in session library.


That's it. Using the above approach, you can set up a login and registration system using CodeIgniter. Using this as base and extend the system as needed.

Originally published on https://www.developer.com.

Page 2 of 2

This article was originally published on April 16, 2012

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date