Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. public function login($username, $password){
  2. //validation
  3. $this->db->select('id, email, username');
  4. $this->db->where('username', $username);
  5. $this->db->where('password', $password);
  6. $this->db->where('status', 1);
  7. $result = $this->db->get('users');
  8. if($result->num_rows() == 1){
  9. return $result->row(0)->id;
  10. } else {
  11. return FALSE;
  12. }
  13. }
  14.  
  15. public function login(){
  16. $data['title'] = 'Login';
  17.  
  18. $this->form_validation-> set_rules('username', 'Username', 'required');
  19. $this->form_validation-> set_rules('password', 'Password', 'required');
  20.  
  21. if($this->form_validation->run() === FALSE){
  22.  
  23. $this->load->view('templates/header');
  24. $this->load->view('users/login', $data);
  25. $this->load->view('templates/footer');
  26. } else {
  27.  
  28. // fetching user
  29. $username = $this->input->post('username');
  30. //Encrypted password
  31. $password = md5($this->input->post('password'));
  32.  
  33. //login user
  34. $user_id = $this->user_model->login($username, $password);
  35.  
  36. if($user_id){
  37. //creating session
  38. $user_data = array(
  39. 'user_id' => $user_id,
  40. 'username' => $username,
  41. 'logged_in' => TRUE,
  42. );
  43.  
  44. $this->session->set_userdata('user_data',$user_data);
  45. // Set message to be sent
  46. $this->session->set_flashdata('user_login', 'Welcome');
  47. redirect('posts');
  48. } else {
  49.  
  50.  
  51.  
  52. // Set message to be sent
  53. $this->session->set_flashdata('login_fail', 'Login Failed');
  54. redirect('users/login');
  55. }
  56.  
  57. }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement