Skip to content

How to resolve MODx session issues

This is a user contributed solution from user Per Holmes:

MODx wraps all session variables, meaning that the MODx session much be active for anything to appear in $_SESSION. This is a problem if the grid itself is loaded embedded in a MODx page, but the Ajax calls go directly to a script. Loading the session will make everything appear to be fine, but the session variables are empty. 

The only way to restore the session that was originally created from within the MODx context is to place the following at the top of phpGrid.php, after the 'use' statements:

require_once('../core/config/config.inc.php');
require_once(MODX_CORE_PATH . 'model/modx/modx.class.php');
$modx= new modX();
$modx->initialize('web');

The ../core/ path assume that the /phpgrid/ is at the top level next to the index.php of MODx. Then it loads the MODx config and instantiates MODx, which reloads the session.

In order to invoke the grid, place the PHP script that creates the grid in e.g. /myassets/mygrid.php. If you load this file directly, it should produce a grid and nothing else.

Then in MODx create a Snippet call RunPHP with the following code:

<?php
require_once($script);

And from within the page where you want the grid to appear, you call

[[!RunPHP? &script=`myassets/mygrid.php`]]

Additionally, it's good to create a .htaccess file in /myassets/.htaccess, so that the grid can't be loaded directly, if e.g. access controls are used within MODx.

order deny,allow
deny from all
allow from 127.0.0.1

Feedback and Knowledge Base