Destroy Session but keep flashdata Destroy Session but keep flashdata codeigniter codeigniter

Destroy Session but keep flashdata


If you try to set flashdata while using a database in the same request after you call sess_destroy(), it won't work (because there is no session to append the flashdata to).

To fix this problem, add $this->ci->session->sess_create(); after the call to sess_destroy(). This works because you're re-creating the session before trying to append data to it. This is the only way to use flashdata after a sess_destroy() if you're using sessions in a database.


The sess_destroy() function destroys also the session flash variables used to pass the message.

U already answered your question, in the library logout() function, you need to replace

$this->ci->session->sess_destroy();

with

$this->ci->session->unset_userdata(array('user_id' => '', 'username' => '', 'status' => ''));

This will not completely destroy the session, only the user data used for login, so I recommend instead, to modify the logout() function in the controller and show the message manually, by passing it to a view.


While this is a workaround, it might do the trick for you...

wherever you're displaying these, I'll be assuming you're checking in the view so...

<? if ($this->session->flashdata('status_messege'): ?>    <p><?= $this->session->flashdata('status_message') ?></p><? endif; ?>

you COULD add an elseif to that and check for the referrer being your logout function...

<? if ($this->session->flashdata('status_messege'): ?>    <p><?= $this->session->flashdata('status_message') ?></p><? else if ($this->agent->referrer() == site_url('path/to/logout'): ?>    <p><?= $this->lang->line('auth_message_logged_out') ?></p><? endif; ?>

A bit of a hackish way to overcome this issue, but probably a way nonetheless.