Header snippet doesn't work with cache

We were unable to figure out how to call our zend framework into kirby. So we had to run kirby in an iframe. Yuk, i know. but for now it’s what we have to work with. Anyways. We run this code in our header.

<?
// Get the full URL of the current page
function current_page_url(){
    $page_url   = 'http';
    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'){
        $page_url .= 's';
    }
    return $page_url.'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
}

//(Assuming session already started)
if(isset($_SESSION['referrer'])){
    // Get existing referrer
    $referrer   = $_SESSION['referrer'];

} elseif(isset($_SERVER['HTTP_REFERER'])){
    // Use given referrer
    $referrer   = $_SERVER['HTTP_REFERER'];

} else {
    // No referrer
}

// Save current page as next page's referrer
$_SESSION['referrer']   = current_page_url();

if(!isset($referrer)){
        header('Location: /resources');
    }


?>

The issue with this is that when cache is enabled it doesn’t work.

//// It works like this

If the user tries to go to our site using http://oursite.com/kirby

it will redirect them to login if they are not.

When cache is turned on.

http://oursite.com/kirby is fully accessable.

Any idea how we can make that script work with cache.

is there a way to ignore a snippet like head. This way it always checks the head script?

With the standard file cache, this is not possible, you can only ignore pages, not parts of pages, I’m afraid.

The reason why your code doesn’t work with the cache is that Kirby only caches the body, not the headers. There is a headers configuration for that kind of stuff, but it isn’t going to help you here.

Best way would be to put the code inside a plugin. Plugins are always loaded, even if the cache is enabled. Please note that you need to exit execution after you set the header, otherwise the HTML is still printed. Kirby has the go() function that does this for you automatically:

go('resources');

I also have a problem with cache and header snippet involving Mobile Detect plugin. After activating the Kirby’s file cache I noticed, that I can only see either mobile or desktop content. This seemingly depends on which content was cached first. I have a navigation element placed in the header that has slightly different content in mobile and desktop. However, also other mobile/desktop specific content, placed elsewhere on the page, does the same thing. Is there a way to fix this issue in my case or is my problem a different case altogether?

The Mobile Detect bit in my header snippet looks like this:

</head>
<body>
	<?php
		if(!detect()->isMobile() || detect()->isTablet()): ?>
		<?php snippet('navigation-desktop') ?>
	<?php endif ?>
	<?php
		if(detect()->isMobile()): ?>
		<?php snippet('navigation-mobile') ?>
	<?php endif ?>

The version of Mobile Detect is the one suggested in this Kirby forum post.

I had a similar problem in the past and solved it by using different cache folders.

c::set('cache', true);
c::set('cache.ignore', array(
  'contact'
));
require_once kirby()->roots()->plugins() . DS. 'kirby-mobile-detect/kirby-mobile-detect.php';
$detect = detect();
if($detect->isMobile() && !$detect->isTablet()) {
    c::set('cache.options', array(
      'root' => kirby()->roots()->index() . DS . 'cache/mobile-cache'
      )
    );
} elseif ($detect->isTablet()){
  c::set('cache.options', array(
    'root' => kirby()->roots()->index() . DS . 'cache/tablet-cache'
    )
  );
}
  else {
  c::set('cache.options', array(
    'root' => kirby()->roots()->index() . DS . 'cache/desktop-cache'
    )
  );
}

Thank you for the suggestion! Is there something I need to adjust to make this work? At the moment I just get an empty screen. Not even the c::set('debug', true); seems to be working. :thinking:

Hm, do you get anything in the PHP error log?

Please note that in my setup above, I put the cache folder into the root of the project, not in the site folder.

Moving the cache folder into the root did the trick :relieved: Many thanks! :grin: