September 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

CodeIgnitor: Create a Login Process, Page 2

  • April 16, 2012
  • 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
$this->session->set_userdata($user_data);
//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.

Conclusion

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.


Tags: PHP, frameworks

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

Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel