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
}
}
}
Have a look at this, and see if this helps.
https://github.com/EllisLab/CodeIgniter/wiki/PK-Session
Edit the config.php
file and set sess_expire_on_close
to true
.
e.g
$config['sess_expire_on_close'] = TRUE;
Add a logout button.
Have that button destroy the session with CI's built in destroy function.
$this->session->sess_destroy();