Logout System in Codeigniter
Hi, friends. In this tutorial, I will give you some source code of session destroy in Codeigniter. So that when a user closes their active account to click log out button, then user go to the login page or another redirect page with the perfect log out system.
A session value destroys when the user clicks on logout button or click on an anchor tag on a web page. In Codeigniter web application to destroy session value, you need to use this code:
$this->session->sess_destroy()
You must be set the User ID or User Email in a session variable when the user completes the login or go to the dashboard after creating a new account. If you not set the session variable so do it first. For help follow this link: store value in a session variable in Codeigniter.
The tutorial, I will show you session value destroy from a session variable and when session value is destroyed then page redirect to login page. So what you need?
First, create a logout link and define a logout function name. The logout function name called in the controller page and unset session variable value using unset_userdata() method.
I will give you the example and source codes of session value destroy in your CodeIgniter web application. Follow the example:
Logout Link:
<a href="<?php echo base_url('Controller/logout'); ?>"> Log Out </a>
Controller:
<?php public function logout(){ $this->session->unset_userdata('user_id'); $this->session->sess_destroy(); redirect('login'); }
You just need to use session library in you controller parent::__construct(). Without session library, you can’t be working with a session. If you need session library then follow the below code and set it fast under parent::__construct() in your controller page.
public function __construct(){ parent::__construct(); $this->load->helper('url'); $this->load->helper('form'); // Model $this->load->model('Model'); // Session $this->load->library('session'); } ?>
I hope it will be helpful for you. We have more tutorial related CodeIgniter, Like: Get MySQL Database Backup in Codeigniter Project
Thanks for visiting our website, we are all time give you the new tutorial. Please follow our website because, DataInFlow is a platform, where you can get the text tutorial and video tutorial of a different kind of programming language like PHP, HTML, Javascript, Jquery, CSS3 and get some technology and latest news.
I Have logout not work
$this->session->sess_destroy();
session_start();
Could you help ?
Apply it
$this->session->unset_userdata(‘session_variable’);
Hope it will be work properly.
wonderful issues altogether, you just received a new reader.
What would you suggest in regards to your submit that you simply made some days ago?
Any certain?