Guest User

Untitled

a guest
Nov 15th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Login extends CI_Controller {
  3. function __construct()
  4. {
  5. parent::__construct();
  6. $this->load->model('login_model','',TRUE); }
  7. public function index() {
  8. $this->load->helper('form');
  9. $this->load->library('form_validation');
  10. $this->form_validation->set_rules('username', 'Username', 'required');
  11. $this->form_validation->set_rules('password', 'Password', 'required');
  12. if ($this->form_validation->run() == FALSE) {
  13. if (isset($this->session->userdata['id'])) {
  14. redirect('home/profile');
  15. } else {
  16.  
  17. //else it will redirect you to login page.
  18. $this->load->view('templates/header');
  19. $this->load->view('login');
  20. $this->load->view('templates/footer');
  21. }
  22. } else {
  23. //if it's a post request with valid data it will validate data in database.
  24. $username = $this->input->post('username');
  25. $password = md5($this->input->post('password'));
  26. $result = $this->login_model->login($username, $password);
  27. //This condition redirect you to homepage if you entered valid credentials
  28. if (count($result) !== 0) {
  29. $this->session->set_userdata('id', $username);
  30. redirect('home/profile');
  31. } else {
  32. //This will redirect you to login page with error.
  33. $this->session->set_flashdata('message', 'Login Fail!!<br>Invalid username or password!!');
  34. redirect('login');
  35. } } } } ?>
  36.  
  37. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  38. class Login_model extends CI_Model
  39. function login($username, $password)
  40. $this->db->select('id','username','password');
  41. $this->db->from('users');
  42. $this->db->where('username', $username);
  43. $this->db->where('password', md5($password));
  44. $this->db->limit(1);
  45. $query = $this->db->get();
  46. if($query->num_rows()>1){
  47. return $query->result();
  48. }
  49. else{
  50. return false;} } }
Add Comment
Please, Sign In to add comment