Session Persistence Issue – Custom Session Variable Not Persisting in Grav

I’m experiencing an issue in Grav where I’m trying to set a custom session variable and then retrieve it on subsequent requests, but the session data always comes back blank. Although Grav reports that the session is started, the session ID is not retained, and no custom data appears in the session array. Every request creates a new session file that only contains default system messages and no trace of my custom variable.

This indicates that, while PHP sessions work correctly in standalone tests, within Grav the session data is not being persisted. It seems that either Grav is regenerating the session on every request or is otherwise preventing custom session variables from being stored.

Has anyone encountered this behavior or have suggestions on how to resolve it?

@mkglass, I cannot reproduce the issue…

  • Using Windows 11 with WSL/Ubuntu 24.04
  • Using a fresh Grav install
  • Using a plugin containing:
      public function onPluginsInitialized(): void
      {
          /** @var Session */
          $session = $this->grav['session'];
          $myValue = $session->myValue;
    
          if (!isset($session->myValue)) {
              $now = date("Y-m-d H:i:s");
              $session->myValue = "myValue = {$now}";
          }
      }
    
  • Using Apache:
    • The latest file created in folder /var/lib/php/sessions/ contains:
      • myValue|s:29:"myValue = 2025-03-14 12:00:47";
  • Using $ bin/grav server
    • The latest file created in folder /var/lib/php/sessions/ contains:
    • myValue|s:29:"myValue = 2025-03-14 12:15:56";
  • In both setups, the session file gets touched after each page refresh, but its name remains the same and myValue remains the same

Please show us your setup and code.

@pamtbaau Hi, thanks for the response.

I’ve tested in two environments:

  1. XAMPP setup on F:\xampp with PHP 8.2.12 (session.save_path = F:\xampp\tmp)
  2. Grav-only setup on C:\grav-test using C:\php 8.1 (session.save_path = c:\tmp)

My system.yaml session settings are:

session:
  enabled: true
  initialize: false
  timeout: 1800
  name: grav-site
  secure: false
  httponly: true
  path: '/'
  split: false
  persistence: true
  allowdata: true

I’m using this Twig test (session-test.html.twig):

{% set session = grav.session %}
    <p>Session started: {{ session.isStarted() ? 'Yes' : 'No' }}</p>
{% if not session.isStarted() %}
    {% do session.start() %}
{% endif %}
{% if session.get('access_granted') is not null %}
    <p>Session variable exists: {{ session.get('access_granted') }}</p>
{% else %}
    {% do session.set('access_granted', true) %}
    <p>Session variable set for the first time.</p>
{% endif %}
<pre>Session Dump: {{ dump(session.all()) }}</pre>

Standalone PHP tests (for sessions and cookies) work fine, but within Grav the session variable never persists and $_COOKIE remains empty.

Thanks!

@mkglass, Please correct the formatting of both you config and code snippets using triple backticks (```).

1 Like

@mkglass, Two remarks:

  • Class Session does not have a set() and get() functions, but uses the magic functions __set() and __get().
    • Try {% do session.__set('access_granted', true) %}
  • Twig is a template engine to generate HTML. I would not use it to alter/set server data.
    • I would prefer to separate the responsibilities and use PHP in either the theme or a plugin to set server data and then “export” server data to Twig using $this->grav['twig']->twig_vars = ['access_granted' => true]
1 Like

The magic methods worked, thank you. I’m going to work on implementing a plugin to handle my session data going forward. Thanks for your help!