Passing Data between Two Snippets III

Passing data between two snippets using a $_SESSION variable


Variable values don't persist across different snippets, even when they're on the same page or in the same template. In the previous article, we looked at using a file (with or without JSON) to pass data to another snippet. In this one, we'll see how to do it with $_SESSION variables.

MODX logo

$_SESSION Variables

$_SESSION variables will not only let you pass data between snippets on a page or template, they also persist across navigation to different pages during the same user session. When you save a $_SESSION variable, it's only available during the current user's session on the site. If you need the data to persist across separate visits to the site (say, on different days), you'd need to use a cookie or save the data to a file or the database. $_SESSION variables are deleted whenever the user closes the browser.


Using $_SESSION Variables to Pass Data from one Snippet to another

To use our example from the previous article in this series, say you want to pass a string created in one custom snippet to another custom snippet. As we mentioned earlier, the best solution here is often to combine the two snippets. For some use cases, though, this is impractical. One alternative we saw in a previous article is to use a file to store the data, but this won't work if simultaneous users need to see different results. Here's how to do it with a $_SESSION variable. Assume that you've created a string you want to pass in the first snippet and placed it in the $string variable. In that first snippet, you'd do this:

$_SESSION['my_saved_string'] = $string);

To retrieve the string in the second snippet, you'd do this:

if (isset($_SESSION['my_saved_string'])) {
    $string = $_SESSION['my_saved_string'];
}

We used isset() in the code above as a sanity check. Without it, PHP will throw an error if the $_SESSION variable is not set. We'll see a better method a little later in this article.


What if I have more than one data item?

In the previous article, where we used a file, we put the data in a PHP array, converted it to a JSON string and saved it. With $_SESSION variables, this is unnecessary. $_SESSION variables can hold arrays (including associative arrays). In the first snippet, do something like this:

$data = array(
    'color' => 'red',
    'size' => 'large',
    'price' => 12.99,
);
$_SESSION['my_saved_array'] = $data;

Then in the second snippet, do this:

$data = $_SESSION['my_saved_data'];

That's all there is to it. You can save as many $_SESSION variables as you like and all will be unique to the user whose session is active when they are saved. Note that the user does not need to be logged in. Every user, even anonymous users, has a unique $_SESSION array.

The $_SESSION variable is actually an array, so when we saved our array above to it, (assuming that it holds nothing else) would look like this:

$_SESSION = array(
  'my_saved_array' => array (
    'color' => "red"
    'size' => "large"
    'price' => 12.99
  )
);

In other words, the $_SESSION array has only one member ('my_saved_array') which contains our array.


If you use a $_SESSION variable to pass data between two snippets on the same page (in the page content, the template, a TV, or a chunk), make sure the receiving snippet tag will be below the sending snippet tag when the page is processed.


A Better Way to Retrieve $_SESSION Data

Because $_SESSION is an array, rather than using isset() to see if the value you want is set, you can use MODX's getOption(), which will work on any array, and lets you specify a default value to use as its second argument:

$data = $modx->getOption($_SESSION, 'my_saved_data', array(), true);

The example above will set $data to an empty array if $_SESSION['my_saved_data'] is not set. The final argument tells MODX to use the default value (array()) if the $_SESSION variable we want is set, but empty. Without it, we could get back an empty string which could cause later code to crash if it's expecting an array.


Coming Up

In the next article, we'll look at passing data to another snippet using a cookie, which will persist across different visits to the site.



Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)