Logout

In this tutorial, we will learn how to create logout.

 

Step 1: You have to go to your Login Controller and create a logout function there. With the help of which you can log out.

eg :

public function logout()
{
	$this->session->unset_userdata('id');

	$this->session->sess_destroy();

	return redirect('login');
}

 

unset : to unset a session variable.

eg : $this->session->sess_destroy();

 

destroy : to destroy whole the session.

eg: $this->session->sess_destroy();

 

Step 2 : Now you have to go to your dashboard page and pass the link of the logout function on your logout button and we will also check if the user has logged in then show logout button otherwise login.

eg :

<?php if($this->session->userdata('id')){ ?>
  //passing logout link 
  <a class="nav-link" href="<?php echo base_url(); ?>login/logout">Logout</a>

<?php }else{ ?>

  <a class="nav-link" href="<?php echo base_url(); ?>login">Login</a>

<?php } ?>

 

Step 3 : We will also check if the user has logged in. So he could not go to the dashboard page without login. For this, we will create a constructor in Dashboard controller, in which we will check that if the user is not logged in then he cannot return to the dashboard page untill he login.

public function __construct()
{
	parent::__construct();

	if(!$this->session->userdata('id')){

		return redirect('login');

	}
}

 


Result :

Source Code:

Codeigniter tutorial for beginners

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

Source Code