Advertisement
Guest User

Untitled

a guest
Apr 10th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.90 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. /**
  5.  * User class.
  6.  *
  7.  * @property  form_validation
  8.  * @extends CI_Controller
  9.  */
  10. class Login extends CI_Controller
  11. {
  12.  
  13.     /**
  14.      * __construct function.
  15.      *
  16.      * @access public
  17.      * @return void
  18.      */
  19.  
  20.     // Each Controller requires a construct to load dependencies
  21.     public function __construct()
  22.     {
  23.         parent::__construct();
  24.         $this->load->library(array('session'));
  25.         $this->load->helper(array('url'));
  26.         $this->load->helper('form');
  27.         $this->load->library('form_validation');
  28.         $this->load->model('User_model');
  29.     }
  30.  
  31.  
  32.     public function index()
  33.     {
  34.  
  35.         $this->login();
  36.  
  37.     }
  38.  
  39.  
  40.     /**
  41.      * login function.
  42.      *
  43.      * @access public
  44.      * @return void
  45.      */
  46.     public function login()
  47.     {
  48.  
  49.         // create the data object
  50.         $data = new stdClass();
  51.  
  52.         // set validation rules
  53.         $this->form_validation->set_rules('username', 'Username', 'required');
  54.         $this->form_validation->set_rules('password', 'Password', 'required');
  55.         $this->form_validation->set_rules('userType', 'User Type', 'required');
  56.  
  57.  
  58.         if ($this->form_validation->run() == false) {
  59.  
  60.             // validation not ok, send validation errors to the view
  61.             $this->load->view('header');  // A
  62.             $this->load->view('users/login');
  63.             $this->load->view('footer');
  64.  
  65.         } else {
  66.  
  67.             // set variables from the form
  68.             $username = $this->input->post('username');
  69.             $password = $this->input->post('password');
  70.             $user_type = $this->input->post('userType'); // Possibly redundant as the user type can be checked in the DB easily and redirected
  71.  
  72.             $test = TRUE; // DEV ONLY
  73.  
  74.             // ####
  75.             // #### Checks to see if password is correct
  76.             // ####
  77.             //if ($this->User_model->resolve_user_login($username, $password)) {
  78.             if ($test) {
  79.  
  80.                 // ####
  81.                 // #### Collects user information
  82.                 // ####
  83.                 //$user_id = $this->User_model->get_user_id_from_username($username);
  84.                 //$user = $this->User_model->get_user($user_id);
  85.  
  86.  
  87.                 // ####
  88.                 // #### Set the Session Data
  89.                 // ####
  90.  
  91.                 //$_SESSION['user_id'] = (int)$user->id;
  92.                 //$_SESSION['username'] = (string)$user->username;
  93.                 $_SESSION['logged_in'] = (bool)true;
  94.  
  95.                 // Compile the page
  96.  
  97.                 $this->load->view('header');
  98.  
  99.                 //TODO: set the redirect on successful login - Determined by the user types ... See line 72
  100.  
  101.                 if ($user_type == 'doctor') {
  102.                     //TODO: There needs to be server side restrictions on these pages
  103.                     redirect('doctor/doctorappointments');
  104.                 } else {
  105.                     redirect('admin');
  106.                 }
  107.  
  108.                 $this->load->view('footer');
  109.  
  110.             } else {
  111.  
  112.                 // login failed
  113.                 $data->error = 'Wrong username or password.';
  114.  
  115.                 // send error to the view
  116.                 $this->load->view('header');
  117.                 $this->load->view('users/login', $data);
  118.                 $this->load->view('footer');
  119.  
  120.             }
  121.  
  122.         }
  123.  
  124.     }
  125.  
  126.     /**
  127.      * logout function.
  128.      *
  129.      * @access public
  130.      * @return void
  131.      */
  132.     public function logout()
  133.     {
  134.         if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
  135.  
  136.             // remove session datas
  137.             foreach ($_SESSION as $key => $value) {
  138.                 unset($_SESSION[$key]);
  139.             }
  140.         }
  141.         $_SESSION['logged_in'] = false;
  142.         // user logout reset to login page (default)
  143.         redirect('/');
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement