Guest User

Untitled

a guest
Dec 12th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. public function login(){
  2. // Prohibit access if already logged in
  3. $this->User_model->session_comprobate_member();
  4.  
  5. $this->form_validation->set_rules('username','Username','trim|required|min_length[4]');
  6. $this->form_validation->set_rules('password','Password','trim|required|min_length[4]');
  7.  
  8.  
  9. if ($this->form_validation->run() == FALSE){
  10. //Load View Into Template
  11. $this->template->load('public','login','users/login');
  12. } else {
  13. // Get Post Data from Database
  14. $username = $this->input->post('username');
  15. $password = $this->input->post('password');
  16. $enc_password = md5($password);
  17. $data_user = $this->User_model->login($username, $enc_password);
  18.  
  19. if($data_user == true){
  20. $user_id = $this->User_model->get_userid($username);
  21. $users = $this->User_model->get_username($user_id);
  22.  
  23. if($users->active == 0){
  24.  
  25. // Create error
  26. $this->session->set_flashdata('error', 'This account is banned or inactive');
  27.  
  28. // Redirect to page
  29. redirect('dashboard/login');
  30.  
  31. }
  32.  
  33. if($users->is_admin == 0){
  34.  
  35. // Create error
  36. $this->session->set_flashdata('error', 'You do not have permission to view this page');
  37.  
  38. // Redirect to page
  39. redirect('dashboard/login');
  40.  
  41. }
  42.  
  43. if($users->is_member == 0){
  44.  
  45. // Create error
  46. $this->session->set_flashdata('error', 'This account does not exists. Please try again.');
  47.  
  48. // Redirect to page
  49. redirect('dashboard/login');
  50.  
  51. } else {
  52. $sess_data = array(
  53. 'user_id' => $user_id,
  54. 'username' => $username,
  55. 'occupation' => 'occupation',
  56. 'is_member' => true,
  57. 'is_admin' => true,
  58. 'active' => true
  59. );
  60.  
  61. // Set Session Data
  62. $this->session->set_userdata($sess_data);
  63.  
  64. // Create Message
  65. $this->session->set_flashdata('success', 'You are logged in');
  66.  
  67. // Redirect to pages
  68. redirect('dashboard');
  69. }
  70. } else {
  71. // Create Error
  72. $this->session->set_flashdata('error', 'Invalid Login');
  73.  
  74. // Redirect to pages
  75. redirect('dashboard/login');
  76. }
  77. }
  78. }
  79.  
  80. $this->session->set_flashdata('error', 'This account is banned or inactive');
  81.  
  82. public function get($id)
  83. {
  84. $this->db->where('id', $id);
  85. $query = $this->db->get($this->table);
  86. return $query->row();
  87. }
  88. public function login($username, $password)
  89. {
  90. $this->db->select('*');
  91. $this->db->from($this->table);
  92. $this->db->where('username', $username);
  93. $this->db->where('password', $password);
  94. $this->db->limit(1);
  95.  
  96. $query = $this->db->get();
  97.  
  98. if ($query->num_rows() == 1) {
  99. return $query->row()->id;
  100. } else {
  101. return false;
  102. }
  103. }
  104.  
  105. //I need to work on these two
  106. public function get_username($users) {
  107. $this->db->select('id');
  108. $this->db->from($this->table);
  109. $this->db->where('username', $users);
  110. return $this->db->get()->row;
  111. }
  112.  
  113. public function get_userid($user_id) {
  114. $this->db->select('id');
  115. $this->db->from($this->table);
  116. $this->db->where('id', $user_id);
  117. return $this->db->get()->row();
  118. }
  119. ///
  120. //Check if admin
  121. public function is_admin($id) {
  122. $this->db->select('is_admin');
  123. $this->db->from($this->table);
  124. $this->db->where('id', $id);
  125. $is_admin = $this->db->get()->row('is_admin');
  126. if ($is_admin == 0) {
  127. redirect('/');
  128. } else {
  129. redirect('admin');
  130. }
  131. }
  132.  
  133. //Check if member
  134. public function is_member($id) {
  135. $this->db->select('is_member');
  136. $this->db->from($this->table);
  137. $this->db->where('id', $id);
  138. $is_member = $this->db->get()->row('is_member');
  139. if ($is_member == 0) {
  140. redirect('/');
  141. } else {
  142. redirect('dashboard/login');
  143. }
  144. }
  145.  
  146. //Check if active
  147. public function is_active($id) {
  148. $this->db->select('active');
  149. $this->db->from($this->table);
  150. $this->db->where('id', $id);
  151. $is_active = $this->db->get()->row('active');
  152. if ($is_active == 0) {
  153. redirect('/');
  154. } else {
  155. redirect('dashboard/login');
  156. }
  157. }
Add Comment
Please, Sign In to add comment