Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <?php
- if(isset($this->session->userdata['is_logged_in'])){
- redirect('autheticate');
- }
- // include 'header.php';
- ?>
- <body>
- <?php
- if(isset($logout_message)){
- echo "<div class='success_message'>";
- echo $logout_message;
- echo "</div";
- }
- ?>
- <div id='content_container'>
- <div id='login_form'>
- <h2>Login Form</h2>
- <hr />
- <?php echo form_open('authentication/login_process'); ?>
- <?php
- echo "<div class='error_message'>";
- if(isset($error_message)){
- echo $error_message;
- }
- echo validation_errors();
- echo "</div>";
- ?>
- <label>Username:</label>
- <input type='text' name='email' id='email' placeholder='Email'/>
- <br /><br />
- <label>Password:</label>
- <input type='password' name='password' id='password' placeholder='********' />
- <br /><br />
- <input type='submit' value=' Login ' name='submit' /> <br />
- <?php echo form_close(); ?>
- </div>
- </div>
- </body>
- </html>
- <?php
- class Authentication extends CI_Controller{
- public function __construct() {
- parent::__construct();
- $this->load->helper('form'); // load the form helper
- $this->load->helper('url_helper'); // load URL helper
- $this->load->library('session'); // load the session library
- $this->load->library('form_validation'); // load the validation libary
- $this->load->model('user_model'); // load the user model
- }
- public function index(){
- $this->load->view('login.php');
- }
- public function login_process(){
- $this->form_validation->set_rules('email', 'Username', 'required|trim|valid_email');
- $this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[8]|max_length[32]');
- if($this->form_validation->run() == FALSE){ // if the login form hasn't been filled out yet
- if(isset($this->session->userdata['is_logged_in'])){ // and if the user is logged in
- if($this->session->userlevel == 1){ // if the user is a student, take them to student home
- $this->load->view('student_home.php');
- }
- else{
- $this->load->view('admin/admin_home.php'); // else, the user is an admin, take them to the admin home
- }
- }
- else{
- $this->load->view('login.php'); // if user is not logged in, display login form
- }
- }
- else{ // else, if the form has been filled out, set variables
- $data = array(
- 'email' => $this->input->post('email'),
- 'password' => $this->input->post('password')
- );
- $result = $this->User_model->login_user($data); // check for authentication
- if($result == TRUE){ // if it works, grab all user info
- $email = $this->input->post('email');
- $result = $this->User_model->get_user_info($email);
- if($result != FALSE){ // if all user info is grabbed, set session data
- $session_data = array(
- 'email' => $result[0]->email,
- 'firstname' => $result[0]->first_name,
- 'userlevel' => $result[0]->user_level_id
- );
- $this->session->set_userdata('is_logged_in', $session_data); // set the session data
- if($this->session->userlevel == 1){
- $this->load->view("student_home.php"); // if they are a student take them to the student home
- }
- else{
- $this->load->view("admin/admin_home.php"); // if they are an admin take the to the admin home
- }
- }
- }
- else{ // if we can't find the username and password match then show error message
- $data = array(
- 'error_message' => 'Invalid Username or Password'
- );
- $this->load->view('login.php', $data);
- }
- }
- }
- public function logout(){
- $session_data = array(
- 'email' => '',
- 'firstname' => '',
- 'userlevel' => ''
- );
- $this->session->unset_userdata('is_logged_in', $session_data);
- $data['logout_message'] = "You have been logged out.";
- $this->load->view('login.php', $data);
- }
- }
Add Comment
Please, Sign In to add comment