採用 PHP Output Control (ob_*) 機制攔截輸出緩衝區,依條件寫入靜態 HTML 檔案,後續請求直接服務靜態檔,大幅降低資料庫與模板引擎負載。本文提供生產級雙階段架構代碼。
<?php
ob_start();
$qstring = $_SERVER['QUERY_STRING'] ?? '';
$qstring = str_replace('=', '', $qstring);
define('HTML_FILE', "./h/{$efilename}" . ($qstring ? "-{$qstring}" : '') . ".html");
if (file_exists(HTML_FILE)) {
$lcft = filemtime(HTML_FILE);
if (($lcft + 3600) > time()) { // 1 小時快取壽命
if ($show_html) {
$go_html ? header("Location: " . HTML_FILE) : echo(file_get_contents(HTML_FILE));
exit;
}
}
}
?>
<?php
require("woods-thtml.php"); // 開啟 OB 與環境初始化
// ... 業務邏輯與模板渲染 ...
require("woods-bhtml.php"); // 關閉 OB、寫入靜態檔、處理相對路徑修正
?>
src/href/action 路徑失效,透過 preg_replace 批次重寫為相對根路徑檔名-QUERY_STRING.html 支援分頁/參數化頁面$go_html 控制 302 導向 vs 直接 echo 內容WoodsTpl 類別,區塊式賦值渲染755,避免腳本注入find -mtime +1 -delete)