[PHP-WEBMASTER] [web-shared] feat/header: Supports setting cache in header

Author: Luffy (sy-records)
Date: 2026-09-02T11:01:15+08:00

Commit: Supports setting cache in header · php/web-shared@cc89d2a · GitHub
Raw diff: https://github.com/php/web-shared/commit/cc89d2a72c0c12c6d082729f9500381a5cb4321c.diff

Supports setting cache in header

Changed paths:
  M templates/header.inc

Diff:

diff --git a/templates/header.inc b/templates/header.inc
index df3a7e9..87f6b3a 100644
--- a/templates/header.inc
+++ b/templates/header.inc
@@ -20,6 +20,10 @@
  * "hidden" => array(array("name" => "hidden-input-name", "value" => "hidden-input-value")),
  * ); // Search box, if any
  * $CURRENT_PAGE // The current page
+ * $CONFIG = array(
+ * "cache" => 1234567890, // optional, timestamp for Last
+ * "cache_control" => 3600, // optional, seconds for Cache-Control
+ * );
  */
isset($TITLE) || $TITLE = "Hypertext Preprocessor";
isset($SUBDOMAIN) || $SUBDOMAIN = "";
@@ -29,8 +33,28 @@ isset($HEAD_RAND) || $HEAD_RAND = "";
isset($CSS) || $CSS = array();
isset($SEARCH) || $SEARCH = array();
isset($CURRENT_PAGE) || $CURRENT_PAGE = "";
+isset($CONFIG) || $CONFIG = array();
$ROOT = substr($_SERVER["SERVER_NAME"], -8) == ".php.net" ? "//shared.php.net" : "/shared";
$current_time = time();
+
+if (isset($CONFIG["cache"])) {
+ if (is_numeric($CONFIG["cache"])) {
+ $timestamp = $CONFIG["cache"];
+ } else {
+ $timestamp = $current_time;
+ }
+ $tsstring = gmdate("D, d M Y H:i:s ", $timestamp) . "GMT";
+
+ if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && $_SERVER["HTTP_IF_MODIFIED_SINCE"] == $tsstring) {
+ header("HTTP/1.1 304 Not Modified");
+ exit;
+ }
+ header("Last-Modified: " . $tsstring);
+}
+
+if (isset($CONFIG["cache_control"]) && is_numeric($CONFIG["cache_control"])) {
+ header("Cache-Control: public, max-age=" . (int) $CONFIG["cache_control"]);
+}
?>
<!DOCTYPE html>
<html>