針對 API 回應或動態頁面,需確保瀏覽器與中繼 Proxy 不快取內容。以下 5 行 Header 設定可完整禁用 HTTP 快取:
header("Content-Type: application/json");
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
| Header | 作用 |
|---|---|
| Expires: 0 | 過期時間設為過去,強制視為過期 |
| Last-Modified | 提供最後修改時間(GMT),配合驗證機制 |
| Cache-Control: no-store, no-cache, must-revalidate | HTTP/1.1 標準:禁止儲存、強制重新驗證 |
| Cache-Control: post-check=0, pre-check=0 | IE 專用:禁用預檢快取 |
| Pragma: no-cache | HTTP/1.0 相容性 |
放在腳本最前端(任何輸出前)即可生效。