Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unsetting some session data in codeigniter

I am saving some data in a session, and certain points in my websites, I am giving the user the option to removes certain parts of the session based on the array key, the array I get when I do,

print_r($this->session->userdata('shortlist'); this leaves me the the following output,

Array ( [0] => Array ( 
    [id] => 40 
    [name] => Namey Name 
    [location] => location is a place 
    [talent] => voice over 
    [image] => ./media/uploads/headshots/width_60_249613_10150280293315435_717615434_9570480_8341358_n.jpg ) );

How can I remove this from my shortlist session? I have tried doing the following, but to no avail,

unset($this->session->userdata('shortlist')[0]);

like image 801
Udders Avatar asked Nov 01 '11 15:11

Udders


People also ask

How can we erase the session data?

A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.

How do I print a session in CI?

print_r($this->session->userdata); or print_r($this->session->all_userdata()); The solution to the same problem, Print All Session In Codeigniter, can also be found in a different method, which will be discussed further down with some code examples. $this->session->set_userdata('some_name', 'some_value');


1 Answers

You can use this:

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

For more info:

http://codeigniter.com/user_guide/libraries/sessions.html

EDIT: After comment: You can do something like this-

$shortlist = $this->session->userdata('shortlist');
unset($shortlist[0]);
$this->session->set_userdata('shortlist',$shortlist);
like image 153
Vikk Avatar answered Sep 20 '22 01:09

Vikk