Guest User

Untitled

a guest
Dec 4th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. $autoload['libraries'] = array('database','session');
  2.  
  3. $route['default_controller'] = 'welcome';
  4.  
  5. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  6. class Welcome extends CI_Controller {
  7.  
  8.  
  9. public function __construct(){
  10.  
  11. parent::__construct();
  12. $this->load->helper('url');
  13. $this->load->model('main_model');
  14. $this->load->library('session');
  15. $this->load->library('form_validation');}
  16. public function index()
  17. {
  18. $this->load->view('welcome_message');
  19. }
  20.  
  21.  
  22. function login_user(){
  23. $this->load->view('welcome_message');
  24.  
  25. } function login_validation(){
  26.  
  27. $this->load->library('form_validation');
  28. $this->form_validation->set_rules('username','Username','required');
  29. $this->form_validation->set_rules('password','Password','required');
  30.  
  31. if($this->form_validation->run()){
  32. // true
  33.  
  34. $username = $this->input->post('username');
  35. $password = $this->input->post('password');
  36.  
  37. // model function
  38. $this->load->model('main_model');
  39.  
  40. if($this->main_model->can_login($username, $password)){
  41. $session_date = array(
  42. 'username'=> $username);
  43.  
  44. $this->session->set_userdata($session_date);
  45.  
  46. redirect(base_url() . 'main/enter');
  47. }else{
  48. $this->session->set_flashdata('error','Invalid Username and Password');
  49. redirect(base_url() . 'main/login_user');
  50.  
  51. }
  52. } else{
  53. //false
  54.  
  55. $this->login_user();
  56. }}function enter(){
  57. if($this->session->userdata('username') !=''){
  58. echo '<h2> Welcome - '.$this->session->userdata('username').'/<h2>';
  59.  
  60. } else{redirect(base_url() . 'main/login_user');}}}
  61.  
  62. function can_login($username,$password){
  63. $this->db->where('UserName',$username);
  64. $this->db->where('Password',$password);
  65. $query=$this->db->get('User');
  66.  
  67.  
  68. if($query->num_rows() > 0){
  69.  
  70. return true;
  71.  
  72. } else{
  73. return false;
  74. }}
Add Comment
Please, Sign In to add comment