Hi All,
I know this topic has been asking in multiple ways, but I things aren’t making sense for me.
In it’s simplest form, I have a raw php file like the following (the value is actually set in a log in page elsewhere - but same thing), and I want to use those $_SESSION variables in snippets, as I’m presently using a combination of MODX and another custom php project of mine.
Here’s the simple php file:
<?php
session_start();
//Comment this out to set the session value.
//$_SESSION['user_id'] = "3";
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo $_SESSION['user_id'];
?>
This can set and display our $_SESSION for the user_id. Easy right?
Well, when I get to Modx, it doesn’t work.
I have a snippet called ‘SessionDebug’ with the following:
<?php
// Start the session if not already started
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Debugging output for session data
$output = 'Session ID: ' . session_id() . '<br>';
$output .= 'Session save path: ' . ini_get('session.save_path') . '<br>';
$output .= 'Session data:<pre>' . print_r($_SESSION, true) . '</pre>';
// Check if user_id is set
if (isset($_SESSION['user_id'])) {
$output .= 'User ID: ' . $_SESSION['user_id'];
} else {
$output .= 'User ID is not set';
}
return $output;
I call it with the following: [[SessionDebug]]
And this is my result:
Session ID: u085ksikgph5df4thfghf6vfni
Session save path: /var/lib/php/session
Session data:
Array
(
[modx.user.0.resourceGroups] => Array
(
[mgr] => Array
(
)
)
[modx.user.0.attributes] => Array
(
[mgr] => Array
(
[MODX\Revolution\modAccessContext] => Array
(
[web] => Array
(
[0] => Array
(
[principal] => 0
[authority] => 0
[policy] => Array
(
[load] => 1
)
)
)
)
[MODX\Revolution\modAccessResourceGroup] => Array
(
)
[MODX\Revolution\modAccessCategory] => Array
(
)
[MODX\Revolution\Sources\modAccessMediaSource] => Array
(
)
[MODX\Revolution\modAccessNamespace] => Array
(
)
)
)
[lockedouttimes] => 0
[counter] => 56
[last_post] => 1717768072
[ip] => XXX.XXX.XXX.XXX
[modx.user.contextTokens] => Array
(
[mgr] => 1
)
[manager_language] => en
[modx.mgr.user.token] => modx65f240426fba20.59374173_1665fd4f4c09b96.80769075
[modx.mgr.session.cookie.lifetime] => 0
[modx.mgr.user.config] => Array
(
)
[newResourceTokens] => Array
(
[0] => 665fd5006ba799.23763600
[1] => 666303d3943c69.07996089
[2] => 66630430aedc35.12033405
[3] => 666304767e9014.51633398
[4] => 6663055d7f9e76.45894429
[5] => 6663142ba57387.80681957
)
)
User ID is not set
I should be getting ‘3’ for the User ID, but it’s not appearing in the $_SESSION value.
When I run the same script again in the raw php version, I get this:
Session ID: u085ksikgph5df4thfghf6vfni
Session save path: /var/lib/php/session
Session data:
Array
(
[lockedouttimes] => 3
[counter] => 46
[last_post] => 1717768216
[ip] => XXX.XXX.XXX.XXX
[user_id] => 3
[user_level] => user
[user_first_name] => Steve
[user_debug] => 0
)
User ID: 3
Here’s the raw php:
<?php
// Start the session if not already started
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Debugging output for session data
$output = 'Session ID: ' . session_id() . '<br>';
$output .= 'Session save path: ' . ini_get('session.save_path') . '<br>';
$output .= 'Session data:<pre>' . print_r($_SESSION, true) . '</pre>';
// Check if user_id is set
if (isset($_SESSION['user_id'])) {
$output .= 'User ID: ' . $_SESSION['user_id'];
} else {
$output .= 'User ID is not set';
}
echo $output;
As you can see, I have the same PHP session ID, and all the other values that are stored in the login session etc., but Modx isn’t passing everything.
Is $_SESSION[‘user_id’] a reserved word or have I just got unlucky?
I don’t have any weird configurations in modx. It’s basically a clean install with a couple of snippets and templates.
Using latest version of Modx v3
Thanks,
Steve