CodeIgniter: unset all userdata, but not destroy the session

codeigniter, codeigniter-2

Solution

This is very simple!

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

or

 $array_items = array('username' => '', 'email' => '');
 $this->session->unset_userdata($array_items);

I hope this helps!

Edit: It looks like you actually don't keep track of anything in your session (kind of strange?). You could call this function I wrote up:

function unset_only() {
    $user_data = $this->session->all_userdata();

    foreach ($user_data as $key => $value) {
        if ($key != 'session_id' && $key != 'ip_address' && $key != 'user_agent' && $key != 'last_activity') {
            $this->session->unset_userdata($key);
        }
    }
}

Of course, that assumes you use the default CI session setup though.

Problem

Is there a way to unset ALL userdata in a session without having to use session destroy? I'm trying to log my user out and need to unset all userdata one at a time. It's becoming tedious to keep track of all possible userdata set in session. I just want to unset everything that might have been set in userdata.

Original source