0

Okay im using codeigniter and i got a session where i want to echo [user_id], i can see its an "Multidimensional Array", but i cant seem to get it right...

This is the print_r from all session userdata:

Array ( 
[session_id] => b7e721332248 
[ip_address] => ::1 
[user_agent] => Mozilla/5.0 
[last_activity] => 1409104940 
[user_data] => [name] => 2 [uniq] => 2 
[flexi_auth] => Array ( [user_identifier] => mail.com [user_id] => 2 [admin] => 1 
[group] => Array ( [3] => Master Admin ) 
[privileges] => Array ( ) 
[logged_in_via_password] => 1 
[login_session_token] => 11017021313jhbjh1h2j3b213mab913269d95 ) )

if i type:

  1. print_r($this->session->all_userdata()); // i will get all
  2. echo $this->session->userdata('uniq'); //returns 2
  3. echo $this->session->userdata('user_id'); //returns nothing(THIS IS THE PROBLEM)

Ive tried something like this: $this->session->userdata(1,1); //1,1 would be array 2 and number 2 which would be user_id

Is it because i can only print out if I loop through them somehow?

1
  • How are you setting the session data? Commented Aug 27, 2014 at 3:40

2 Answers 2

1

You're trying to call the data directly, if you're want to access user_data or flexi_auth you need to assign the data to a variable then target it since they're arrays.

$session_data = $this->session->all_userdata();
echo $session_data['user_data']['uniq']; // 2

$user_data = $this->session->userdata('user_data');
echo $user_data['uniq']; /// 2

$flexi_auth = $this->session->userdata('flexi_auth');
echo $flexi_auth['user_id']; /// 2

If you're trying to target a string directly

echo $this->session->userdata('last_activity');
Sign up to request clarification or add additional context in comments.

1 Comment

$flexi_auth = $this->session->userdata('flexi_auth'); echo $flexi_auth['user_id']; //this is it
1

You can simply access user name stored in session like this

echo $this->session->userdata['user_data']['name'];

Be sure you have some userdata in session or use like this to avoid any errors

echo @$this->session->userdata['user_data']['name'];

adding @ does not generate any errors. hope you got the answer you were looking for.

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.