Guest User

Untitled

a guest
May 3rd, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. public function login() {
  2. $data = $this->data;
  3. $email = $this->input->post('email');
  4. $password = md5($this->input->post('password'));
  5. $result = $this->user_model->login($email, $password);
  6.  
  7. if (count($result) !== 0) {
  8. $this->session->set_userdata('user_id', $email);
  9. $seid = $this->session->userdata('user_id');
  10. if ($seid == '') {
  11. redirect(site_url());
  12.  
  13. } else {
  14. redirect('home/view');
  15. }
  16. } else {
  17. redirect('home/index');
  18. }
  19. }
  20.  
  21. function login($email, $password) {
  22. $this->db->where("email", $email);
  23. $this->db->where("password", $password);
  24. $query = $this->db->get("user");
  25. return $query->result_array();
  26. }
  27.  
  28. <html xmlns="http://www.w3.org/1999/xhtml">
  29. <head>
  30. <title>Simple Login with CodeIgniter</title>
  31. </head>
  32. <body>
  33. <h1>Simple Login with CodeIgniter</h1>
  34. <?php echo validation_errors(); ?>
  35. <?php echo form_open('verifylogin'); ?>
  36. <label for="username">Username:</label>
  37. <input type="text" size="20" id="username" name="username"/>
  38. <br/>
  39. <label for="password">Password:</label>
  40. <input type="password" size="20" id="passowrd" name="password"/>
  41. <br/>
  42. <input type="submit" value="Login"/>
  43. </form>
  44. </body>
  45. </html>
  46.  
  47. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  48.  
  49. class VerifyLogin extends CI_Controller {
  50.  
  51. function __construct()
  52. {
  53. parent::__construct();
  54. $this->load->model('user','',TRUE);
  55. }
  56.  
  57. function index()
  58. {
  59. //This method will have the credentials validation
  60. $this->load->library('form_validation');
  61.  
  62. $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
  63. $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
  64.  
  65. if($this->form_validation->run() == FALSE)
  66. {
  67. //Field validation failed. User redirected to login page
  68. $this->load->view('login_view');
  69. }
  70. else
  71. {
  72. //Go to private area
  73. redirect('home', 'refresh');
  74. }
  75.  
  76. }
  77.  
  78. function check_database($password)
  79. {
  80. //Field validation succeeded. Validate against database
  81. $username = $this->input->post('username');
  82.  
  83. //query the database
  84. $result = $this->user->login($username, $password);
  85.  
  86. if($result)
  87. {
  88. $sess_array = array();
  89. foreach($result as $row)
  90. {
  91. $sess_array = array(
  92. 'id' => $row->id,
  93. 'username' => $row->username
  94. );
  95. $this->session->set_userdata('logged_in', $sess_array);
  96. }
  97. return TRUE;
  98. }
  99. else
  100. {
  101. $this->form_validation->set_message('check_database', 'Invalid username or password');
  102. return false;
  103. }
  104. }
  105. }
  106. ?>
  107.  
  108. <?php
  109. Class User extends CI_Model
  110. {
  111. function login($username, $password)
  112. {
  113. $this -> db -> select('id, username, password');
  114. $this -> db -> from('users');
  115. $this -> db -> where('username', $username);
  116. $this -> db -> where('password', MD5($password));
  117. $this -> db -> limit(1);
  118.  
  119. $query = $this -> db -> get();
  120.  
  121. if($query -> num_rows() == 1)
  122. {
  123. return $query->result();
  124. }
  125. else
  126. {
  127. return false;
  128. }
  129. }
  130. }
  131. ?>
  132.  
  133. public function login(){
  134.  
  135. $email=$this->input->post('email');
  136.  
  137. //query the database
  138. $result = $this->mod_user->login($email, $password);
  139. if($result){
  140. $sess_array = array();
  141. foreach($result as $row){
  142. $sess_array = array(
  143. 'user_id' => $row->user_id,
  144. 'email' => $row->email
  145. );
  146. $this->session->set_userdata('sess', $sess_array);
  147. }
  148. return TRUE;
  149. }
  150. else
  151. {
  152. $this->form_validation->set_message('login', 'Invalid username or password');
  153. return false;
  154. }
  155. }
  156.  
  157.  
  158. function login($userName, $pass){
  159. $this -> db -> select('user_id, email, password');
  160. $this -> db -> from('tbl_users');
  161. $this -> db -> where('email', $email);
  162. $this -> db -> where('password', MD5($password));
  163. $this -> db -> limit(1);
  164.  
  165. $query = $this -> db -> get();
  166.  
  167. if($query -> num_rows() == 1)
  168. {
  169. return $query->result();
  170. }
  171. else
  172. {
  173. return false;
  174. }
  175. }
  176.  
  177. public function login() {
  178. $this->load->helper('form');
  179. $this->load->library('form_validation');
  180.  
  181. $this->form_validation->set_rules('email', 'Email', 'required');
  182. $this->form_validation->set_rules('password', 'Password', 'required');
  183.  
  184. // This condition check whether the request is post with valid data or not if it's not a post request than form validation return FALSE
  185. if ($this->form_validation->run() == FALSE) {
  186.  
  187. // This condition check if session id is set or not if session id is set it will redirect you to homepage.
  188. if (isset($this->session->userdata['user_id'])) {
  189. redirect('home/view');
  190. } else {
  191.  
  192. //else it will redirect you to login page.
  193. $this->load->view('login_view');
  194. }
  195. } else {
  196.  
  197. //if it's a post request with valid data it will validate data in database.
  198. $email = $this->input->post('email');
  199. $password = md5($this->input->post('password'));
  200. $result = $this->user_model->login($email, $password);
  201.  
  202. //This condition redirect you to homepage if you entered valid credentials
  203. if (count($result) !== 0) {
  204. $this->session->set_userdata('user_id', $email);
  205. redirect('home/view');
  206. } else {
  207.  
  208. //This will redirect you to login page with error.
  209. $this->session->set_flashdata('message', 'Login Fail!!<br>Invalid username or password!!');
  210. redirect('login');
  211. }
  212. }
  213. }
  214.  
  215. $this->db->select('*');
  216. $this->db->where($info);
  217. $this->db->from($table);
  218. $query = $this->db->get();
  219. if($query->num_rows() > 0){
  220. $row = $query->row_array();
  221. return $row;
  222. }
  223. }
  224.  
  225. if(isset($_POST['login'])){
  226.  
  227. $log['email'] = $_POST['email'];
  228. $log['password'] = $_POST['password'];
  229. $details = $this->User_model->fetchrowlogin($log,'candidate');
  230.  
  231. if(count($details)){
  232. $ids = $details['id'];
  233. $email = $details['email'];
  234. $fname = $details['fname'];
  235.  
  236. $this->session->set_userdata(array(
  237.  
  238. 'custid' => $ids,
  239. 'emailid'=> $email,
  240. 'fname'=> $fname,
  241. ));
  242. redirect('http://localhost/test27/index.php/welcome/dashboard');
  243. }else{
  244.  
  245. redirect(base_url().'front1');
  246.  
  247.  
  248. }
  249.  
  250. }
  251.  
  252.  
  253.  
  254. $this->load->view('front/login');
  255. }
Add Comment
Please, Sign In to add comment