Delete php session when closing browser

Asked

Viewed 792 times

4

I’m using Codeigniter to develop a project that involves a restricted area.

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

I need this information to be deleted when closing 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 I’m reading the session:

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

How I am doing session deletion while logging in by button:

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

My config.php configuration:

$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 constructor check:

public function __construct() {
    parent::__construct();
    if (!isset($this->session->associated_hash) || empty($this->session->associated_hash)) {
        redirect('login');
    }
}
  • Questions: When you have the session deleted, does it delete and therefore not enter the restricted pages ? and When you close the browser still persists the session, yes or no? Like I’m asking you because it’s your local problem and I’m trying to figure it out!?!?

    1. This, if I delete the session I can no longer access. 2. Yes, if I close, the session continues.
  • is something local... it is difficult to reproduce, I say this because what has to be done in the documentation reports! and has no other option...

  • 1

    I came to the conclusion that it is a bug q happens in Ubuntu’s Firefox. I tested in Chrome on Ubuntu and Chrome and Firefox on Windows.

1 answer

5


Goes in the file application/config/config.php, search for $config['sess_expiration'] and assign the value 0, as demonstrated just 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 explains 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.

translating

| 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.
  • I did the tests here and did not delete the session :/ I will edit the question to show my session settings (it is the same as yours)

  • 1

    @Bernardokowacic sincerity I’ve already made site with Codeigniter and the setting is this same, now what are you doing? because, you have to close the browser completely not only the tab!!! understood

  • @Bernardokowacic even that is in the documentation as explained. Now it will really depend on what you are doing! can count step by step.

  • So basically, when the user accesses the site, he will be able to login to access the restricted area, when he informs email and password, I create the session using: $this->Session->set_userdata('associated_hash', $userdata[0]->hash). When accessing the restricted page it checks the hash in the function that is executed when loading the page to see if it exists and if it is valid, for this use: $this->Session->associated_hash. If he wants to log out I use: $this->Session->unset_userdata('associated_hash').

  • Put the code in your question of the two methods! @Bernardokowacic, including what checks whether you are logged in or not!

  • updated the question with the code

  • @Bernardokowacic put that if in your class builder

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.