Appending Data To Existing CodeIgniter Session Variable

April 15th, 2013 - Posted by Steve Marks to PHP, Web Development.

When storing information in a session, it is likely that you may want to add additional inormation to this session at a later date. For this particular post I’ll be focusing on how to achieve this within CodeIgniter. Allow me therefore to first demonstrate how the session we will be extending may be getting initialised in the first place:

$data = array(
	'username' => 'joebloggs',
	'name' => 'Joe Bloggs',
	'email' => ''

$this->session->set_userdata('user', $data);

In the above code we are creating a session called ‘user’, then assigning an array to it containing the users information.

Now, lets imagine that at a later date we also want to add a phone number to this session. We can do this easily using the method outlined below:

$data = $this->session->userdata('user');

$data['telno'] = "01234 567 890";

$this->session->set_userdata('user', $data);

If you can’t make out what we’re doing here, we’re assigning the existing user data to a variable called $data. We then add an extra element to the array, in our case this is the user’s telephone number, then overwrite the existing user session with the new infrmation.

This entry was posted on Monday, April 15th, 2013 at 2:38 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (1)
  1. moko says:

    thank you, it works ..