Starting modx revolution sessions in external php scripts
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4122
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
This is how you start the default modx sessions from external php scripts, in other words, scripts that needs sessions and need the sessions to be the same as the ones that modx revolution starts in itself.

Thanks very much to for the working solution, which was correct. You can also find the solution at this modx revolution sessions thread. Please be aware that the method used in Modx Evolution is NOT the same. The modx evolution method is this, just so you avoid confusion. This is NOT, again, the method you will use for modx revolution:

:: Code ::
require_once '.../manager/includes/';
function CheckAuthentication()
   return $_SESSION['mgrValidated']==1 ? true : false;

The real modx revolution session start from external scripts that run outside of modx pages

This is the real solution:

:: Code ::
// Include files to declare the MODx core class
require_once '/path_to_your/config.core.php'; 
require_once MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php'; 
require_once MODX_CORE_PATH.'model/modx/modx.class.php'; 

// Create an instance of the MODx core
$modx = new modX();

// Initialise the core to access the default 'web' context

// Set a value in the desired session variable

You can set the first value that is gotten from: require_once '/path_to_your/config.core.php';
which just declares two modx globals, like this:
:: Code ::
define('MODX_CORE_PATH', '/full/path/to/modxcore/' );
define('MODX_CONFIG_KEY', 'config');

which would give you:
:: Code ::

define('MODX_CORE_PATH', '/full/path/to/modxcore/' );
define('MODX_CONFIG_KEY', 'config');
require_once ( MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php' ); 
require_once ( MODX_CORE_PATH.'model/modx/modx.class.php' ); 

// Create an instance of the MODx core
$modx = new modX();

// Initialise the core to access the default 'web' context

I'm running a very complex external application that needs a lot of session support, and this fix was the solution, now all the little glitches are gone. Simply create an include and add this to the top of each php file that needs it. Works, and solves the session issue, which as far as I know, is not possible to resolve without this method.

Important: you must accept the default modx session name, do not try to mess with that. That's probably going to be PHPSESSID, but verify this by checking your session cookies for the site, do not assume it.

Important: do not use session_start in php, the $modx->initialize('web'); line starts the session internally in modx, you can't and shouldn't try to interfere with that native process, just use it.

Also don't use anything like: session_name() except to get the actual session name used if you need to use that.

After you set this, you can set and read all $_SESSION['your_session_variaable'] data just like anywhere else.

Remember also that modx revolution does not allow use of SID in query strings to pass session data, so you have to do explicit cookie support tests also and handle those for users with cookies disabled.

Thanks to the site that solved this issue.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours