CodeIgniter Forums
unset_userdata - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: unset_userdata (/showthread.php?tid=2903)



unset_userdata - El Forum - 08-30-2007

[eluser]Colin Carter[/eluser]
Hello,

I'm not sure if I'm using unset_userdata() properly, but if I wanted to unset a number of session variables using an array unset_userdata() doesn't work.

For example:
Code:
$this->session->unset_userdata(array('name', 'data'));

doesn't unset the 'name' and 'data' session variables. This is, I believe, due to the line in unset_userdata() unsetting based on an array key not its value.

Code:
unset($this->userdata[$key]);

if I'm using unset_userdata() incorrectly please could you let me know how to use it. I read that it's been documented in svn but I couldn't find how to check it out.

Many thanks


unset_userdata - El Forum - 08-30-2007

[eluser]johnwbaxter[/eluser]
The html page in the userguide


unset_userdata - El Forum - 08-30-2007

[eluser]xwero[/eluser]
Is it already implemented in the current release? The current userguide doesn't have the unset_userdata section.


unset_userdata - El Forum - 08-30-2007

[eluser]johnwbaxter[/eluser]
No the current userguide does not have it documented, the code is however present in the current released codebase (1.5.4) and you can use it just fine.


unset_userdata - El Forum - 08-30-2007

[eluser]Colin Carter[/eluser]
There's also a typo in this documentation:

Quote:Removing Session Data

Just as set_userdata() can be used to add information into a session, unset_userdata() can be used to remove it, by passing the session key. For example, if you wanted to remove 'some_name' from your session information:

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

This function can also be passed an array of items to unset.

$array_items = array('username', 'email');

$this->session->set_userdata($array_items);

Code:
$this->session->set_userdata($array_items);
shoud read:
Code:
$this->session->unset_userdata($array_items);

but this won't work because 'username' and 'email' are values and unset_userdata() unsets based on their keys (0 & 1)


unset_userdata - El Forum - 08-30-2007

[eluser]johnwbaxter[/eluser]
Have you e-mailed derek allard to let him know?


unset_userdata - El Forum - 08-30-2007

[eluser]Colin Carter[/eluser]
here's my fixed version:

Code:
function unset_userdata($newdata = array())
{
   if (is_string($newdata))
   {
      $newdata = array($newdata);
   }

   if (count($newdata) > 0)
   {
      foreach ($newdata as $val)
      {
         unset($this->userdata[$val]);
      }
   }

   $this->sess_write();
}



unset_userdata - El Forum - 08-30-2007

[eluser]johnwbaxter[/eluser]
I suggest using OBsession, i use it and it works great.


unset_userdata - El Forum - 08-30-2007

[eluser]Colin Carter[/eluser]
OBsession uses the same unset_userdata() function


unset_userdata - El Forum - 08-30-2007

[eluser]johnwbaxter[/eluser]
Nice, i'll admit i haven't passed an array to that function yet. I'll have to bear this in mind.

Thanks.