CodeIgnitor: Create a Login Process, Page 2
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; } }
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.
Originally published on https://www.developer.com.
Page 2 of 2