1

So I have stored my session data into the session like below:

        $user_data = array(
          'user_id' => $user_id,
          'email' => $email,
          'logged_in' => true
        );

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

In my view, I'm trying to echo the email out onto the page but no matter which way I try to access it, it will not echo out:

echo $this->session->userdata('email');

How can I access the array values?

1 Answer 1

3

You are storing the values in an array & then assigning that array to a variable called login_session but while retrieving the data you are just accessing with only key and not with array['key'].

So the correct syntax is:

echo $this->session->userdata('login_session')['email'];

OR

$log_sess=$this->session->userdata('login_session');
echo $log_sess['email'];

Please go through the codeigniter document as well.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I did try ...->userdata(['login_session']['email']); but it didn't work. Also, there is nothing in the documentation detailing this! (I couldn't find it anyway) Thanks again!
You are Welcome!

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.