Session variable change on page reload

Ok so, I have a little special Kirby setup where I store products in a database and use custom routes to build up the urls for the product pages. I then have a category tree menu build by pages and subpages that show a collection of products based on some tags stored along the products in the db. The category tree menu is “closed” from the beginning and sub categories is shown when the user clicks on a category and go to that page.

When a user then decides to click on one of the products they go to the product page thanks to the custom router that pull the right product data from the db. But on this page the category tree menu is back to its closed state because kirby does not know what category the user clicked on before choosing a product. How do I best solve this?

I prefer not to use url params for this due to seo and keeping the product url clean. So I thought sessions would be a good ide to store the uri of the last category that was open before going to the product page. So at the top of my category template I put

$kirby->session()->set('priskriget.categoryHistory', $page->uri());

$categoryHistory = $kirby->session()->get('priskriget.categoryHistory', false);
echo $categoryHistory; // just to see what got stored

And then at the top of my product template I put

$categoryHistory = $kirby->session()->pull('priskriget.categoryHistory', false);
echo $categoryHistory;

The first time I go to a category page I can see that that uri is stored in the session, perfect. Then when I click on a product and go to the product page that same uri is still in the session. Nice! But then if i reload the product page the uri from the session get changes to another category uri. Why? Is the session code from my category template running on the product page? I probably just don’t understand how sessions works but a little help on this would be much appreciated.

Hm, according to the documentation, pull() fetches the current value and then removes it. While that doesn’t explain why you still get a value, you should probably use get() if you want the value to still be there after page reload.

Ah, sorry. I tried pull just to see if that worked better. Used get first with the same reload “bug”

I even tried to reset the session in my product template, but still the same strange on reload.

// templates/products.php
$categoryHistory = $kirby->session()->get('priskriget.categoryHistory', false);
$kirby->session()->set('priskriget.categoryHistory', $categoryHistory);

echo $categoryHistory;

Do you have any steps to reproduce this in a Starterkit? If I set a session value in one template and retrieve it in another template, then I can reload the page as many times as I want without the value disappearing. So I’d assume you have some code somewhere that sets a different value. But hard to tell without knowing what’s happening in your code.

You are right. In the startkit it works as you expect. I can’t under stand what is happening, it’s super weird. If I in the product template change to another session name

// templates/product
$categoryHistory = $kirby->session()->get('priskriget.test', false);
echo $categoryHistory;

and go to a product page the $categoryHistory is empty as exspected.

If I then add this code to the home template for example:

// templates/home
$kirby->session()->set('priskriget.test', $page->uri());

$categoryHistory = $kirby->session()->get('priskriget.test', false);
echo $categoryHistory;

and now reload the product page twice (first the $categoryHistory is empty) the uri “home” is displayed. So it’s like the code from the home template is running even on the product page. Don’t even know where to start to find this bug.

Could it be something with my setup? This is my index.php file

include dirname(__DIR__) . '/kirby/bootstrap.php';
include dirname(__DIR__) . '/site/snippets/domains.php';

$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';

$url  = Url::host();
$baseRoot = dirname(__DIR__);
$storageRoot = dirname(__DIR__) . '/storage';
$domainRoot = dirname(__DIR__) . '/storage/' . $domains[$url];
$mediaRoot = __DIR__ . '/media/' . $domains[$url];
$mediaUrl = $protocol . $url . '/media/' . $domains[$url];

$kirby = new Kirby([
  'roots' => [
    'index'     => __DIR__,
    'site'      => $baseRoot . '/site',
    'accounts'  => $storageRoot . '/accounts',
    'cache'     => $domainRoot . '/cache',
    'sessions'  => $domainRoot . '/sessions',
    'content'   => $domainRoot . '/content',
    'media'     => $mediaRoot
  'urls' => [
    'media' => $mediaUrl

echo $kirby->render();

I now tried the same index.php and folder structure on the Startkit as on my site, and the Startkit still works as it should. How can I debug this?

Looks like it might be something with my local server setup. I use MAMP Pro and if I use the “localhost/sitefolder” url it all works, but if I set up a custom local domain (host) in MAMP and point that to the sitefolder and use that local domain in the browser the weird reload bug is back.

I now tried it on my live server and the bug is there also… So it’s not only my local env. Any ideas on where to look next is very much appreciated.

I find the bug! I forgot that I use a little js ( to preload content from links based on what is in the user’s viewport. So thats what made the session update to random uri :exploding_head: :exploding_head: :exploding_head:

Glad you finally found it, these things are impossible to debug from remote.