9
votes

hi i would like to ask if it is possible to update the session data saved in the database in codeigniter,, for example. i have a session userdata(roleID,name,logged_in), so that when someone will login, ill just call the $data['name'] = $this->session->userdata('name'); and echo it in my header view as <?php echo $name; ?>,, the problem is when a user will update his firstname or lastname, and when i do this

$fname = $this->input->post('fname');
$lname = $this->input->post('lname');

$fullname = $fname." ".$lname;
$this->session->unset_userdata('name');
$this->session->set_userdata('name',$fullname);

it doesnt work..

//EDIT WORKING RIGHT NOW... JUST TYPO AND SYNTAX ERRROR

2
that second unset should read set_ right ?David Chan
oh im so dumb..its working ayt now.. :) thankskester martinez

2 Answers

22
votes

If you want to update the session data, use:

$this->session->set_userdata('name', $fullname);

There is no need to use unset_userdata More info here

0
votes

You don't redeclare session again, just use the code:

$this->session->set_userdata('session_variable_name', $newvalue);

Example:

New session declaration:

$this->session->set_userdata(
               array(
               'year'  =>  2017
                ));

Updating current session value 'year' into '2018':

$this->session->set_userdata('year', 2018);