Display Username after Login

In this tutorial, I will teach you, how to create display user name after login. It  means when user  login is  success. then we will show user name on dashboard.

 

Step 1 : Go to Login model and get the user details after login succes.

eg: 

<?php
class Login_model extends CI_Model 
{
	
	public function login_data($user, $pass)
	{
		//here i am fetching data from login table using where condition
		$query = $this->db->where(['name'=>$user,'pass'=>$pass])
						  ->get('login');

		$login = $query->row();

		//return user details
		return $login;
	}

}

?>

 

Step 2 : Go to Login Controller. So that  you will pass user details on session.

eg :

<?php
class Login extends CI_Controller {
	
	public function index()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('uname','Username','required');
		$this->form_validation->set_rules('password','Password','required');
		if($this->form_validation->run())
		{
			$user = $this->input->post('uname');
			$pass = $this->input->post('password');
            
			$this->load->model('login_model');

			$login = $this->login_model->login_data($user, $pass);

			if($login){

				//here i am passing user details in session
				$data = array();
				$data['id'] = $login->id;
				$data['name'] = $login->name;
				$this->session->set_userdata($data);
				return redirect('dashboard');
			}
			else{
				$this->session->set_flashdata('fail_login','Invalid Credentials');
				return redirect('login');
			}
		}
		else
		{
			$this->load->view('login');
		}
		
	}

}

?>

 

Step 3 : Go to Dashboard controller and load dashboard view page. 

<?php
class Dashboard extends CI_Controller {
	
	public function index()
	{
		
		$this->load->view('dashboard');
		
	}

}

?>

 

Step4 : Open dashboard view page and show user name after login with the help of session userdata.

<h2> Welcome <?php echo $this->session->userdata('name'); ?> </h2>

 


Result :

Source Code:

Codeigniter tutorial for beginners

In this project. We are providing you, how to create small project in Codeign....

Source Code