Guest User

Untitled

a guest
Dec 6th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. <html>
  2.  
  3. <?php
  4. if(isset($this->session->userdata['is_logged_in'])){
  5. redirect('autheticate');
  6. }
  7.  
  8. // include 'header.php';
  9.  
  10. ?>
  11. <body>
  12. <?php
  13. if(isset($logout_message)){
  14. echo "<div class='success_message'>";
  15. echo $logout_message;
  16. echo "</div";
  17. }
  18. ?>
  19.  
  20. <div id='content_container'>
  21. <div id='login_form'>
  22. <h2>Login Form</h2>
  23. <hr />
  24. <?php echo form_open('authentication/login_process'); ?>
  25. <?php
  26. echo "<div class='error_message'>";
  27. if(isset($error_message)){
  28. echo $error_message;
  29. }
  30. echo validation_errors();
  31. echo "</div>";
  32. ?>
  33. <label>Username:</label>
  34. <input type='text' name='email' id='email' placeholder='Email'/>
  35. <br /><br />
  36. <label>Password:</label>
  37. <input type='password' name='password' id='password' placeholder='********' />
  38. <br /><br />
  39. <input type='submit' value=' Login ' name='submit' /> <br />
  40. <?php echo form_close(); ?>
  41. </div>
  42. </div>
  43. </body>
  44. </html>
  45.  
  46. <?php
  47.  
  48.  
  49. class Authentication extends CI_Controller{
  50.  
  51. public function __construct() {
  52. parent::__construct();
  53. $this->load->helper('form'); // load the form helper
  54. $this->load->helper('url_helper'); // load URL helper
  55. $this->load->library('session'); // load the session library
  56. $this->load->library('form_validation'); // load the validation libary
  57. $this->load->model('user_model'); // load the user model
  58. }
  59.  
  60. public function index(){
  61. $this->load->view('login.php');
  62. }
  63.  
  64. public function login_process(){
  65. $this->form_validation->set_rules('email', 'Username', 'required|trim|valid_email');
  66. $this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[8]|max_length[32]');
  67.  
  68. if($this->form_validation->run() == FALSE){ // if the login form hasn't been filled out yet
  69. if(isset($this->session->userdata['is_logged_in'])){ // and if the user is logged in
  70. if($this->session->userlevel == 1){ // if the user is a student, take them to student home
  71. $this->load->view('student_home.php');
  72. }
  73. else{
  74. $this->load->view('admin/admin_home.php'); // else, the user is an admin, take them to the admin home
  75. }
  76. }
  77. else{
  78. $this->load->view('login.php'); // if user is not logged in, display login form
  79. }
  80. }
  81. else{ // else, if the form has been filled out, set variables
  82. $data = array(
  83. 'email' => $this->input->post('email'),
  84. 'password' => $this->input->post('password')
  85. );
  86. $result = $this->User_model->login_user($data); // check for authentication
  87.  
  88. if($result == TRUE){ // if it works, grab all user info
  89. $email = $this->input->post('email');
  90. $result = $this->User_model->get_user_info($email);
  91.  
  92. if($result != FALSE){ // if all user info is grabbed, set session data
  93. $session_data = array(
  94. 'email' => $result[0]->email,
  95. 'firstname' => $result[0]->first_name,
  96. 'userlevel' => $result[0]->user_level_id
  97. );
  98.  
  99. $this->session->set_userdata('is_logged_in', $session_data); // set the session data
  100. if($this->session->userlevel == 1){
  101. $this->load->view("student_home.php"); // if they are a student take them to the student home
  102. }
  103. else{
  104. $this->load->view("admin/admin_home.php"); // if they are an admin take the to the admin home
  105. }
  106. }
  107. }
  108. else{ // if we can't find the username and password match then show error message
  109. $data = array(
  110. 'error_message' => 'Invalid Username or Password'
  111. );
  112. $this->load->view('login.php', $data);
  113. }
  114. }
  115. }
  116.  
  117. public function logout(){
  118. $session_data = array(
  119. 'email' => '',
  120. 'firstname' => '',
  121. 'userlevel' => ''
  122. );
  123. $this->session->unset_userdata('is_logged_in', $session_data);
  124. $data['logout_message'] = "You have been logged out.";
  125. $this->load->view('login.php', $data);
  126. }
  127.  
  128. }
Add Comment
Please, Sign In to add comment