Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Session Persistence Issue – Custom Session Variable Not Persisting in Grav

Solved by pamtbaau View solution

Started by Michael Glass 1 year ago · 5 replies · 118 views
1 year ago

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?

last edited 03/13/25 by Michael Glass
1 year ago

@mkglass, I cannot reproduce the issue...

  • Using Windows 11 with WSL/Ubuntu 24.04
  • Using a fresh Grav install
  • Using a plugin containing:

    PHP
    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.

1 year ago

@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:

YAML
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):

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!

last edited 03/14/25 by Michael Glass
1 year ago

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

👍 1
last edited 03/14/25 by pamtbaau
1 year ago Solution

@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
last edited 03/14/25 by pamtbaau
1 year ago

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!

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 60 16 hours ago
Support · by Anna, 3 days ago
2 66 18 hours ago
Support · by Justin Young, 19 hours ago
1 33 19 hours ago
Support · by Duc , 1 week ago
2 69 6 days ago
Support · by Colin Hume, 1 week ago
2 61 6 days ago