application/controllers/Auth.php
/* 
		Check if the login form is submitted, and validates the user credential
		If not submitted it redirects to the login page
	*/
Check if the login form is submitted, and validates the user credential
If not submitted it redirects to the login page
Check if the login form is submitted and validates the user credential If not submitted it redirects to the login page
public function login()
	{
		$this->logged_in();
		$this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        if ($this->form_validation->run() == TRUE) {
           	$username_exists = $this->model_auth->check_username($this->input->post('username'));
           	if($username_exists == TRUE) {
           		$login = $this->model_auth->login($this->input->post('username'), $this->input->post('password'));
           		if($login) {
           			$logged_in_sess = array(
           				'id' => $login['id'],
				        'username'  => $login['username'],
				        'logged_in' => TRUE
					);
					$this->session->set_userdata($logged_in_sess);
           			redirect('dashboard', 'refresh');
           		}
           		else {
           			$this->data['errors'] = "<div class='alert alert-danger alert-dismissible' role='alert'>
					   <button type='button' class='close' data-dismiss='alert' aria-label='close'><span aria-hidden='true'>&times;</span></button>
					   <strong>Invalid Username or Password</strong></div>";
           			$this->load->view('login', $this->data);
           		}
           	}
           	else {
           		$this->data['errors'] = "<div class='alert alert-danger alert-dismissible' role='alert'>
				   <button type='button' class='close' data-dismiss='alert' aria-label='close'><span aria-hidden='true'>&times;</span></button>
					<strong>Invalid Username or Password</strong></div>";
           		$this->load->view('login', $this->data);
           	}	
        }
        else {
            $this->load->view('login');
        }	
	}
public function login ( ) { $ this -> logged_in ( ) ; $ this -> form_validation -> set_rules ( 'username' , 'Username' , 'required' ) ; $ this -> form_validation -> set_rules ( 'password' , 'Password' , 'required' ) ; if ( $ this -> form_validation -> run ( ) == TRUE ) { $ username_exists = $ this -> model_auth -> check_username ( $ this -> input -> post ( 'username' ) ) ; if ( $ username_exists == TRUE ) { $ login = $ this -> model_auth -> login ( $ this -> input -> post ( 'username' ) , $ this -> input -> post ( 'password' ) ) ; if ( $ login ) { $ logged_in_sess = array ( 'id' => $ login [ 'id' ] , 'username' => $ login [ 'username' ] , 'logged_in' => TRUE ) ; $ this -> session -> set_userdata ( $ logged_in_sess ) ; redirect ( 'dashboard' , 'refresh' ) ; } else { $ this -> data [ 'errors' ] = " <div class='alert alert-danger alert-dismissible' role='alert'>
					   <button type='button' class='close' data-dismiss='alert' aria-label='close'><span aria-hidden='true'>&times;</span></button>
					   <strong>Invalid Username or Password</strong></div> " ; $ this -> load -> view ( 'login' , $ this -> data ) ; } } else { $ this -> data [ 'errors' ] = " <div class='alert alert-danger alert-dismissible' role='alert'>
				   <button type='button' class='close' data-dismiss='alert' aria-label='close'><span aria-hidden='true'>&times;</span></button>
					<strong>Invalid Username or Password</strong></div> " ; $ this -> load -> view ( 'login' , $ this -> data ) ; } } else { $ this -> load -> view ( 'login' ) ; } }
Check if the login form is submitted, and validates the user credential
If not submitted it redirects to the login page
Check if the login form is submitted and validates the user credential If not submitted it redirects to the login page
// true case // false case