Destroy the session, but save flashdata

I use Tank Auth to manage users in my CI 1.7.3 application. Everything works fine, but I'm trying to set flash_message to display when the user logs out. The problem is that the function $this->tank_auth->logout(); destroys the session. I changed the exit function in the Tank Auth library to look like this:

  function logout() { $this->delete_autologin(); // See http://codeigniter.com/forums/viewreply/662369/ as the reason for the next line $user_session_data = array('user_id' => '', 'username' => '', 'status' => ''); $this->ci->session->set_userdata($user_session_data); $this->ci->session->unset_userdata($user_session_data); } 

Earlier it was

 function logout() { $this->delete_autologin(); // See http://codeigniter.com/forums/viewreply/662369/ as the reason for the next line $this->ci->session->set_userdata(array('user_id' => '', 'username' => '', 'status' => '')); $this->ci->session->sess_destroy(); } 

In my controller I have

 function logout(){ if ($this->tank_auth->is_logged_in()) { // logged in $this->session->set_flashdata('status_message', $this->lang->line('auth_message_logged_out')); $this->tank_auth->logout(); redirect(''); } 

}

If I remove the function $this->tank_auth->logout(); , the message is displayed in order. I'm sure this is a simple session issue

+4
source share
3 answers

If you try to install flashdata when using the database in the same query after calling sess_destroy() , this will not work (because there is no session to add flashdata).

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

+6
source

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

U already answered your question, in the logout() library, 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 to log in, so I recommend that you instead change the logout() function in the controller and show the message manually, passing it to the view.

+2
source

Although this is a workaround, it can do the trick for you ...

wherever you display them, I will assume that you check the view like this ...

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

you COULD add elseif to this and make sure the referrer is your exit 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 little hacky way to overcome this problem, but probably nonetheless.

+1
source

Source: https://habr.com/ru/post/1344015/


All Articles