PHP 會話導致每個頁面都包含標題cache-control: no-store, no-cache, must-revalidate。
我需要覆寫此行為并將其更改為Cache-Control: s-maxage=10, public, max-age=10甚至只是cache-control: public, max-age=10.
我嘗試使用會話變數session_cache_limiter('public');,session_cache_expire(1);但是過期值以分鐘為單位,我無法弄清楚如何將其設定為 10 秒。
如何設定session_cache_expire(1);為 10 秒?如果那不可能,我還能如何覆寫會話標頭并將快取控制設定為 10 秒?
uj5u.com熱心網友回復:
在 PHP 中,您使用 header() 函式:
<?php
header("Cache-control: public, max-age=10");
header("Expires: Sat, 1 Apr 2022 05:00:00 GMT");
?>
替代選項是在頂部使用 HTML 標記 - 在第一次 <?php 打開之前:
<!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.
如果您使用任何具有模板的前端框架 - 您應該在那里查找元標記,或者可能在該框架檔案中查找元標記(可能有專門的功能)。
uj5u.com熱心網友回復:
在@Amikot40 和@CBroe 的幫助下,我解決了這個問題。
// 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");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/447226.html
