Delete php session when closing browser

3

I'm using CodeIgniter to develop a project that involves a restricted area.

I am using session to store the data of the user that is logged in.

I need to close this browser when I close the browser, remembering that I am using CodeIgniter version 3.1.6 sessions.

Example of how I created the sessions:

$this->session->set_userdata('associated_hash', $userData[0]->hash);

How do I read the session:

public function index() {
    if (!isset($this->session->associated_hash) || empty($this->session->associated_hash)) {
        redirect('login');
    }
    ...
}

How do I delete the session when I clicked the button:

$this->session->unset_userdata('associated_hash');

My configuration in config.php:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 0;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

Edit with check in builder:

public function __construct() {
    parent::__construct();
    if (!isset($this->session->associated_hash) || empty($this->session->associated_hash)) {
        redirect('login');
    }
}
    
asked by anonymous 19.12.2017 / 20:47

1 answer

4

Go to the file application/config/config.php , search for $config['sess_expiration'] and assign the value 0 , as shown below:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 0; // para quando fechar o navegador a sessão expire
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

In your description already explain this, note:

  

'sess_expiration'

|   The number of SECONDS you want the session to last.
|   Setting to 0 (zero) means expire when the browser is closed.

translation

| O número de SEGUNDOS que você deseja que a sessão dure.
| A configuração para 0 (zero) significa expirar quando o navegador está fechado.
    
19.12.2017 / 21:06