The $session API variable

This API variable provides access to read/write of session variables, login and logout of users, redirects, and more.

The $session API variable is provided to every template automatically. The session is started automatically and ready-to-use from your templates. It is not used with scripts accessing the API externally since ProcessWire is not in control of the session in that instance.

Setting and retrieving a session variable

It is very easy to use $session for maintaining persistent storage throughout the user's session. ProcessWire automatically starts session management on every request, so there is nothing you need to do to enable it... it's already running. Thus, to set a session variable, you would just do this:

$session->hello = "Hello World!"; 

Then you will be able to retrieve that value for any other page requests from the same user during this session:

echo $session->hello;  // displays: Hello World!

Unless you have changed something about PHP's session handling functions, the values are stored at the server side. The values retrieved from $session can be considered as safe as the values you set to it… they are not open to remote manipulation like cookie values.

Note: if you prefer, you can use get() and set() methods with $session instead of direct access.

$session function reference

See the full $session API reference in the comprehensive ProcessWire API reference.

Iterating $session

When you iterate $session, it cycles through all of your set session variables:

foreach($session as $name => $value) {
  echo "<p>$name =  $value</p>";
}

$session data storage

Session variables are currently stored with PHP's session functions with files in /site/assets/sessions/. This directory is protected from HTTP access by ProcessWire's .htaccess file. You can also install the SessionHandlerDB module (included with the core) which makes ProcessWire use database-driven sessions instead.

$session vs $_SESSION

You may also use PHP's $_SESSION superglobal variable in ProcessWire, but note that $session uses a different namespace within $_SESSION, so the two can't be used interchangeably for the same variables... it's best to stick to using one or the other, and we recommend using $session.

Twitter updates

  • ProcessWire 3.0.142 has a lot of updates but the biggest is the addition of custom fields support for file and image fields. In this post, we take a closer look at that, and also outline all of the new features in the just-released FormBuilder v40— More
    11 October 2019
  • This week’s dev branch version brings you improvements to ProcessWire’s input->cookie API variable, plus it adds the ability to modify system URLs and paths at runtime. This post also includes some examples to demonstrate just how useful this can be— More
    20 September 2019
  • New post: This week we’ll take a look at a new version of FormBuilder that's on the way (with a screencast), as well as the latest version of the core: ProcessWire 3.0.140— More
    6 September 2019

Latest news

  • ProcessWire Weekly #283
    The 283rd issue of ProcessWire Weekly introduces ProcessWire 3.0.142 and FormBuilder v40, highlights some conference content, and features a new weekly poll continuing with the #processwireworks theme. Read on!
    Weekly.pw / 12 October 2019
  • ProcessWire 3.0.142 core updates
    This latest version of the core on the dev branch has a lot of updates, bnd the biggest is the addition of custom fields support for file and image fields. In this post, we take a closer look and also outline all of the new features in the just-released FormBuilder v40.
    Blog / 11 October 2019
  • Subscribe to weekly ProcessWire news

“ProcessWire is like a breath of fresh air. So powerful yet simple to build with and customise, and web editors love it too.” —Margaret Chatwin, Web developer