how do i reset the value of a single session array index in codeigniter?

Related searches

Using a user model which returns an array that looks like this:

$user_data['display_name'] = "John Doe";

$user_data['avatar'] = ./images/user144.jpg";

i create my session using $this->session->set_userdata('user_data',$user_data);

now if on another controller i let the user change his avatar,

how can i replace the session variable associated to that?

like $this->session->set_userdata('user_data["avatar"]',$new_avatar); just wont work right?

hee thanks for the help...


From looking at an overview of your code, I'm guessing the best way to go about this is to unset the data and reset it.

Use $this->session->unset_userdata('thesessiontounset'); Then set it back up with the new information and old.

how do i reset the value of a single session array index in codeigniter?, Any piece of information from the session array is available through the If you want to add session data one value at a time, set() also supports this syntax: To clear the current session (for example, during a logout), you may simply use either CREATE INDEX "ci_sessions_timestamp" ON "ci_sessions" ("timestamp"​);. The first argument, some_name, is the name of the session variable, under which, some_value will be stored. set_userdata() function also supports another syntax in which you can pass array to store values as shown below.


The session->set_userdata() function will only let you modify one key at a time. In your case the key refers to an array so what you're trying to do isn't possible in the way you're attempting to do it.

When I'm updating my session I run something like this.

//Create or setup the array of the fields you want to update.
$newFields = array('avatar' = > 'image01.png');

//Check to see if the session is currently populated. 
if (!is_array($this->session->userdata('abc'))){
    //...and if it's not - set it to a blank array
    $this->session->set_userdata('abc',array());
}

//Retrieve the existing session data
$existing_session = $this->session->userdata('abc');

//Merge the existing data with the new data
$combined_data = array_merge($this->session->userdata('abc'), $newFields);
//update the session
$this->session->set_userdata('abc',$combined_data);

More details on array_merge can be found here

Session Library, Session data is simply an array associated with a particular session ID (cookie). If you want to add userdata one value at a time, set_userdata() also supports this To clear the current session (for example, during a logout), you may simply use CREATE INDEX "ci_sessions_timestamp" ON "ci_sessions" ("timestamp");. Users with sharp eyes would have noticed that you could also assign an array as a value of any session variable, as shown above in the interests example. Now, let's see how to retrieve the value of any session variable.


First controller

$user_data['display_name'] = "John Doe";

$user_data['avatar'] = "./images/user144.jpg";

$this->session->set_userdata('user_data',$user_data);

Second controller

$user_data = $this->session->userdata('user_data');

$user_data['avatar'] = $new_avatar;

$this->session->set_userdata('user_data', $new_avatar);

Session Library, //which one is correct to unset a full session array name $userdata? which is the key to the var to unset, or an array, not the variable itself  For the most part the session class will run unattended in the background, so simply initializing the class will cause it to read, create, and update sessions when necessary. To initialize the Session class manually in your controller constructor, use the $this->load->library () method: $this->load->library('session');


It is a bit late, but it might be useful to someone else, this seems to work:

$this->session->userdata['user_data']['avatar'] = $new_avatar;
$this->session->userdata['other_data']['other'] = $other;
$this->session->sess_write();

This allows you to edit values in array just like you do with $_SESION['user_data']['avatar'] = $avatar, with 'only' one extra line and only using CI library.

unset session array, Any piece of information from the session array is available through the If you want to add session data one value at a time, set() also supports this syntax: To clear the current session (for example, during a logout), you may simply use INDEX "ci_sessions_timestamp" ON "ci_sessions" ("timestamp");. Session Library. The Session class permits you to maintain a user’s “state” and track their activity while they browse your site. CodeIgniter comes with a few session storage drivers, that you can see in the last section of the table of contents: Using the Session Class. Initializing a Session.


For Unset Session variable

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

For Set Session variable

$this->session->set_userdata('avatar', '/images/user144.jpg"');

CodeIgniter gives you access to a Query Builder class. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases, only one or two lines of code are necessary to perform a database action.


You can use the asterisk as a wildcard to replace any of the segments. When found, it will search through all of the child nodes until it finds it. This is handy if you don’t know the values, or if your values have a numeric index:


You can have the results returned as an instance of a custom class instead of a stdClass or array, as the result() and result_array() methods allow. This requires that the class is already loaded into memory. The object will have all values returned from the database set as properties.


Include both local and server at the same time. php. file_exists: http://php.net/manual/en/function.file-exists.php is_dir: http://php.net/manual/en/function.is-dir