PHP session is causing each page to contain the header cache-control: no-store, no-cache, must-revalidate
.
I need to override this behavior and change it to Cache-Control: s-maxage=10, public, max-age=10
or even just cache-control: public, max-age=10
.
I've tried to use session variables session_cache_limiter('public');
and session_cache_expire(1);
but, the expire value is in minutes and I cant work out how to set it to 10 seconds.
How can I set session_cache_expire(1);
to 10 seconds?
If that's not possible, how else can I override the session header and set the cache control to 10 seconds?
CodePudding user response:
In PHP you use header() function:
<?php
header("Cache-control: public, max-age=10");
header("Expires: Sat, 1 Apr 2022 05:00:00 GMT");
?>
Alternate option is to use HTML tags on top - before first <?php opening:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Cache-control" content="max-age=10" />
<meta http-equiv="Expires" content="Sat, 1 Apr 2022 05:00:00 GMT" />
</head>
<?php
// your php code starts here.
If you using any frontend framework which has its templates - you should look for meta tags there, or possibly in that framework documentation (possibly there are dedicated functions for that).
CodePudding user response:
With the help of @Amikot40 and @CBroe, I've solved the issue.
// remove pragma no-cache header with session variable
// this also sets the Cache-Control header, but you will change that after session start
session_cache_limiter('public');
session_start();
// cache for 10 seconds
header("Cache-Control: s-maxage=10, public, max-age=10");
// expire in 10 seconds
$expire_time = new DateTime('UTC');
$expire_time->add(new DateInterval('PT10S')); // add 10 seconds
$expire_time = $expire_time->format(DateTimeInterface::RFC7231);
header("Expires: $expire_time");