How to destroy session with browser closing in codeigniter
You can use javascript and asynchron request.
When you close the window, the handler of window.onunload is called
var unloadHandler = function(e){
//here ajax request to close session
};
window.unload = unloadHandler;
To solve the problem of redirection, php side you can use a counter
class someController extends Controller {
public function methodWithRedirection(){
$this->session->set_userdata('isRedirection', 1);
//here you can redirect
}
}
class homeController extends Controller{
public function closeConnection(){
$this->load->library('session');
if($this->session->userdata('isRedirection')!== 1){
//destroy session
}
}
}
How to destroy session with browser closing in codeigniter 3
You just have to set 'sess_expiration' to 0, as described in the manual and in the config.php comments.
Note: Technically, you can't really destroy the session when the browser is closed. You can only tell the browser to discard the session cookie after it is closed, but the session itself is still usable on the server-side (i.e. if you are the victim of a MITM attack and somebody stole the session ID).
The session is actually deleted later by the garbage collector.
How to prevent CodeIgniter sessions from being destroyed when the browser is closed?
Hope this will help you :
Set in $config
like this
$config['sess_expire_on_close'] = FALSE;
To know more : https://www.codeigniter.com/user_guide/libraries/sessions.html
Related Topics
Save Curl Display Output String in Variable PHP
Tcpdf and Insert an Image Base64 Encoded
Derived Class Defined Later in the Same File "Does Not Exist"
How to Change the Name of an Element in Dom
Count Word Frequency in a Text
Clear Data in MySQL Table with PHP
PHP Foreach() with Arrays Within Arrays
Destroy PHP Session on Page Leaving
Http Auth via PHP - PHP_Auth_User Not Set
Switch of PHP Versions Not Working on MAC
Model->Save() Not Working in Yii2
How to Use PHP Inside CSS File
PHP Datetime::Createfromformat in 5.2
How to Make a Select in PHP/MySQL Case Insensitive
Moving from MySQL to MySQLi or Pdo
Soapclient: How to Pass Multiple Elements with Same Name
Get Last Word from Url After a Slash in PHP
Need to Merge Multiple PDF's into a Single PDF with Table of Contents Sections