Code for handle login controller

 

class Login extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->checklogin();
		$this->load->helper('form');
		$this->load->view('frontend/login');
	}
	
	
	public function validate() {
		
		if($this->input->post('submit')) {
			
			$this->load->library('form_validation');
			$this->form_validation->set_rules('username', 'Email', 'required|trim');
			$this->form_validation->set_rules('password', 'Password', 'required');
			
			$this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');
			
			if($this->form_validation->run())
			{
				// If validation passes
				$username = $this->input->post('username');
				$password = $this->input->post('password');
				
				$this->load->model('loginmodel');
				
				$user_info = $this->loginmodel->login_valid( $username, md5($password ));
				if( $user_info )
				{
					if($user_info->status == 0) {
						
						$this->session->set_flashdata('login_failed', 'Account Inactive! Please Contact Admin');
						//return redirect('login');
						$this->load->view('frontend/login');
						
					} else {
						
						$this->session->set_userdata('user_info', $user_info);
						return redirect('admin/dashboard');
					
					}
					
				}
				else
				{
					// Authentication Failed
					// echo "Password Do not Match";
					$this->session->set_flashdata('login_failed', 'Invalid Username/Password.');
					//return redirect('login');
					$this->load->view('frontend/login');
				}
			}
			else
			{
				// failed
				$this->load->view('frontend/login');
				//echo 'Validation Failed';
				//echo validation_errors();
			}
			
		} else {
			return redirect('login');
		}
	}
	
	
	// Admin Logout
	public function logout()
	{
		//$this->load->helper('url');
		$this->session->unset_userdata('user_info');
		return redirect ('login');
	}