Problem with session

I’m using the Kirby session to store a value for the view mode of the site. This value is modified via an ajax call when you click on the change view button. It seems to work just fine in my local copy and on the server: the view value is successfully saved and kept until the next page load, as desired.

However, for some strange reason, the same is not the case for my client. When he navigates the same server version of the site, the view value is always the initial value. I’ve outputted some values through the console for him to check and he has confirmed that the ajax call gets the correct values returned.

I thought the session was a server thing, so I don’t get why two users would get different outcomes when navigating the same site.

This is the code I have in my controller:

if( isset( params()['view'] ) ) {	
	$view = params()['view'];
} elseif( $kirby->session()->get( 'view' ) ) {
	$view = $kirby->session()->get( 'view' );
} else {
	$view = $site->find( 'preferences' )->view();
$kirby->session()->set( 'view', $view );

And a similar code is executed at the time of the ajax call:

$view = 'hints';
if( get( 'params' ) == 'changeViewToHints' ) { //params is what JS sends
	$view = 'hints';
} elseif( get( 'params' ) == 'changeViewToLinear' ) {
	$view = 'linear';

$kirby->session()->set( 'view', $view );

$json = [
	'result' => (string)$kirby->session()->get( 'view' )

So I output result to the console and both me and my client see the expected value. The difference is in my case it stays and is outputted by the next $kirby->session()->get( 'view' ) while in the case of my client, it doesn’t, and outputs the initial value.

But yet this is the first time I use Kirby Sessions so maybe I’m missing on something very obvious. Could somebody guide me in how to debug this?


Apart from the issue: Wouldn’t it make more sense to use cookies instead of storing this value in the session?

:face_with_monocle: Hmm, good point. I was just concerned I would have to add an “allow cookies?” message, but after some research about it, that’s not necessary for “strictly necessary” cookies like this one. I’ll try cookies and if the problem persists, I’ll come back.

kirby uses php sessions. these are unique for each visitor. further information

By default, session variables last until the user closes the browser.

about kirby and cookies. the kirby cookie helper adds a hash to its value. this is good if you only use it in php but it means you can not use it to exchange data from js to kirby using that helper.

this is what i usually do:


Cookies.set('cookie-notice', '1', {
          expires: Infinity,
          domain: window.location.hostname,
          secure: (window.location.protocol === 'https:')


$cookiesAccepted = isset($_COOKIE['cookie-notice']) ? intval($_COOKIE['cookie-notice']) === 1 : false;

Thanks for the heads up, @bnomei!

What I do is manipulate the cookie using PHP code that I execute through a JS ajax call. So even though JS is part of the operation, it doesn’t mess with the cookie directly. If I understand correctly, this means I can safely rely on Kirby’s Cookie Class, right?

If you don’t do anything with the cookie in JS, yes.

I got it working using:

Had to still use Kirby’s Cookie::remove() to get rid of the old cookie, which, even though had the same name as the new one, the browser would consider them different. But that was presumably due to how the domain was written in the cookie (for some reason your code window.location.hostname gives me a domain starting with a dot, like

Couldn’t Kirby have more documentation on how to use the Cookies helper? Is it written anywhere that a hash is added to the cookie value?

Yes, I agree, it’s a bit thin.