4

I store few data in session as the following:

$session_data = array("uid" => "test user", "loged_in" => true);

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

To modify the "uid" I tried

$uid = array("uid" => "New user");
$this->session->set_userdata($uid);

It did not work so I tried

 $this->session->set_userdata("uid","New user");

It also did not work. Cant find any useful stuff on google. Please help how can I change values in the session??

2
  • Your code is correct (aside from a couple of typos). I'm guessing there's an issue somewhere else. Are you using db to store session details? What is the output of session data when you call $this->output->enable_profiler(TRUE);? Commented Oct 3, 2011 at 14:09
  • whats with all the typos? 'logged_in' (2 g's) also things like 'array' (not arra) Commented Oct 3, 2011 at 14:20

3 Answers 3

11

Did you load your session library? $this->load->library('session'); It might be a dumb question but it does not hurt to ask.

What about setting your encryption key? I imagine you would see an error message for that https://www.codeigniter.com/user_guide/libraries/encryption.html

$session_data = array('uid' => 'test user', 'logged_in' => TRUE);
$this->session->set_userdata($session_data);

// modify session
$this->session->set_userdata('uid', 'New user');
Sign up to request clarification or add additional context in comments.

Comments

2

Aside from all your typos (of course those matter in programming), you might want to see if you are just confusing yourself and typo'ing the array/value/key name incorrectly:

Do the following:

echo "<pre>";
print_r($this->session->all_userdata());
echo "</pre>";

and after doing that you will be one step closer to knowing what typo's or problems you ran into as that will display your session array:

Array
(
    [session_id] => 4a5a5dca22728fb0a84364eeb405b601
    [ip_address] => 127.0.0.1
    [user_agent] => Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7;
    [last_activity] => 1303142623
    [uid]        => 2
    etc...
)

Comments

1
///set session data
$data = array('Id' => 'test Id', 'is_logged_in' => TRUE);
$this->session->set_userdata($data);

// modify session data
$this->session->set_userdata('Id', 'New test Id');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.