How to Destroy Session with Browser Closing in Codeigniter

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



Leave a reply



Submit