Guest User

Untitled

a guest
Jun 29th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 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. parent::__construct();
  5. $this->load->model('login_model','',TRUE);
  6. }
  7.  
  8. public function index() {
  9. $this->load->helper('form');
  10. $this->load->library('form_validation');
  11. $this->form_validation->set_rules('username', 'Username', 'required');
  12. $this->form_validation->set_rules('password', 'Password', 'required');
  13. if ($this->form_validation->run() == FALSE) {
  14. if (isset($this->session->userdata['id'])) {
  15. redirect('home/profile');
  16. } else {
  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. }
  38. }
  39. ?>
  40.  
  41. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  42. class Login_model extends CI_Model{
  43. function login($username, $password)
  44. $this->db->select('id','username','password');
  45. $this->db->from('users');
  46. $this->db->where('username', $username);
  47. $this->db->where('password', md5($password));
  48. $this->db->limit(1);
  49. $query = $this->db->get();
  50.  
  51. if($query->num_rows()>1){
  52. return $query->result();
  53. }else{
  54. return false;
  55. }
  56. }
  57. }
  58.  
  59. if( $query->num_rows() == 1 ){
  60. return $query->result();
  61. }
  62.  
  63. $this->db->where('password', md5($password));
  64.  
  65. $this->db->where('password', $password);
Add Comment
Please, Sign In to add comment