CodeIgniter中如何正确访问数组形式存储的$_SESSION数据
Hey there, let's work through these session problems you're facing! The errors you're seeing all point to how you're storing data in the session rather than how you're trying to access it—let's break this down step by step.
First, Let's Understand the Errors
- "Object of class stdClass could not be converted to string": This means
$_SESSION['userID']isn't a plain number or string—it's a full PHP object (likely the result of a database query). When you try to use it like a string, PHP throws this error. $this->session->firstnamefailing: This is probably a version mismatch. CodeIgniter 3.x uses$this->session->userdata('firstname')to access session values, not direct property access. For CodeIgniter 4+, it'ssession()->get('firstname').- "Invalid argument supplied for foreach()": You're trying to loop over
$_SESSION['userID'], but it's an object (not an array), so foreach can't process it.
The Root Cause: How You're Saving the Session
Almost certainly, your controller is storing the entire user query result (an object or multidimensional array) into the session instead of extracting individual fields. For example, if your code looks like this:
// ❌ Wrong: Storing the full user object/array $user = $this->Users_model->get_user($some_id); $_SESSION['userID'] = $user;
That's why var_dump($_SESSION) shows a multidimensional structure—you're saving the whole user record, not just the userID value.
Fixes to Try
1. Correctly Store Individual Session Fields
When saving user data to the session, extract each property you need instead of saving the entire object/array. Here's how:
// ✅ Correct: Extract specific fields from the user result $user = $this->Users_model->get_user($some_id); // If $user is an object (common with CodeIgniter's query results) $_SESSION['userID'] = $user->userID; $_SESSION['email'] = $user->email; $_SESSION['firstname'] = $user->firstname; // If $user is an array (if your model returns arrays instead of objects) $_SESSION['userID'] = $user['userID']; $_SESSION['email'] = $user['email']; $_SESSION['firstname'] = $user['firstname'];
This way, each session key holds a simple scalar value (string/number) that you can access directly with $_SESSION['userID'] without errors.
2. Verify Your Session Configuration (config.php)
Make sure your session settings are properly configured to avoid loading issues:
// Example config for CodeIgniter 3.x $config['sess_driver'] = 'files'; $config['sess_cookie_name'] = 'ci_session'; $config['sess_expiration'] = 7200; $config['sess_save_path'] = APPPATH . 'cache/sessions/'; // Ensure this folder exists and is writable $config['sess_match_ip'] = FALSE; $config['sess_time_to_update'] = 300;
Also, ensure the Session library is loaded:
- Auto-load it in
autoload.php:$autoload['libraries'] = array('session'); - Or load it manually in your controller:
$this->load->library('session');
3. Access Session Data the Right Way
- Using native
$_SESSION: Once you've stored scalar values, you can access them directly like you wanted:echo $_SESSION['userID']; echo $_SESSION['email']; - Using CodeIgniter's Session Class:
- For CodeIgniter 3.x:
$this->session->userdata('userID'); - For CodeIgniter 4+:
session()->get('userID');
- For CodeIgniter 3.x:
4. Debug to Confirm Session Structure
If you're still unsure, run var_dump($_SESSION) in your controller to see exactly what's stored. For example, if you see:
array(3) { ["userID"]=> string(2) "42" ["email"]=> string(15) "tim@example.com" ["firstname"]=> string(3) "Tim" }
That's exactly what you want—each key maps to a simple value. If you see an stdClass object or nested array, go back to step 1 and fix how you're saving the session.
内容的提问来源于stack exchange,提问作者Tim C

