3
votes

i need your help. I used the session to record the user selected business type in CI. For example,$this->ci->session->set_userdata('biztype','food'). When user login,it works ok. However, once the user logout, session will be destroyed in the function logout().So i set the userdata again in the function logout().You can view the code below:

function logout()
{
  $biztype = $this->ci->session->userdata('biztype');
  $this->delete_autologin();

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

  $this->ci->session->sess_destroy();
  $this->ci->session->set_userdata('biztype',$biztype);
  //echo $this->ci->session->userdata('biztype');   //here, i can get biztype that i want
}

However,when i logout and redirect to homepage, i cant get the userdata('biztype') and my session_id have changed. Thanks for the help.

3
Sorry, i am trying to get the point. You log out a user but keep it's biztype after redirection to the homepage to use for what?Murat Ünal

3 Answers

1
votes

This is straight from CodeIgniter User Guide:

Destroying a Session

To clear the current session:

$this->session->sess_destroy();

Note: This function should be the last one called, and even flash variables will no longer be available. If you only want some items destroyed and not all, use unset_userdata().

So no, you cannot destroy a session then add user_data to it, you need to reload / redirect then once the NEW session is established add data.

Try using cookies for peristance, or use the mentioned unset_userdata() fn.

0
votes

$this->session->sess_destroy() ;

This function should be called only at the end of the execution. For unsetting data (as you're trying to do) it's better to use unset_userdata method. See how you should implement that:

$unset_items = array('user_id' => '', 'username' => '', 'status' =>  '') ;

$this->ci->session->unset_userdata( $unset_items ) ;
0
votes
   $email  = "[email protected]";

   ///set the session 
   use the set_userdata function and include the session library

   $this->load->library('session');

   $this->session->set_userdata('session name',Value);
   i.e.
   $this->session->set_userdata('email', $email);

   //unset the session 
   $this->session->unset_userdata('session name');
   i.e.
   $this->session->unset_userdata('email');