【问题标题】:How to Turn off Caching of PHP Pages on Server and Browser?如何关闭服务器和浏览器上 PHP 页面的缓存?
【发布时间】:2021-02-27 03:55:43
【问题描述】:

我不确定 php 页面的缓存从何而来。到目前为止,我已禁用缓存;

Chrome 的开发者工具> 网络> 禁用缓存;

Debian 的 \opt\php73\etc\php73.ini with;

 opcache.enable=0
 opcache.enable_cli=0

我已添加到我的服务器条目的末尾;

fastcgi_cache_bypass $is_args;
  fastcgi_no_cache $is_args;

我在 php 脚本的顶部有这个标题;

 header("Cache-Control: no-cache");

但我仍然无法弄清楚缓存的来源。

有人可以推荐我可能看的其他地方吗?

【问题讨论】:

  • 你怎么知道你的页面被缓存了?有什么没有按预期工作吗?
  • opcache 与pages 无关……最好重新开启。
  • 如果您解释实际问题,我们也许可以帮助您解决。
  • 一些代码会有所帮助,尤其是在您的 css 和脚本包含时,当您声明它们时,而不是实际源代码。可能这些是缓存的罪魁祸首。如果确实如此,您可以附加一个在每次重新加载时更改的参数。图片也是。

标签: php linux caching server debian


【解决方案1】:

如果您尝试使用 PHP 停止网页缓存,我使用以下代码

//Minimize caching so admin area always displays latest statistics
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
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");

虽然没有什么是真正充分的证据,因为许多 CSS 和其他 JS 文件是独立于 PHP 进行缓存的,并且一旦更新需要刷新才能显示,还有其他方法可以解决这个问题。

我使用的一个示例是对文件进行哈希处理(使用 php):

$specific_page = '/assets/javascripts/pages/javascript.js';
if(file_exists($specific_page)) {
    $html .= '<script src="'.$specific_page.'?v='.hash_file('crc32',$specific_page).'"></script>';
}

这将保留缓存的文件,直到文件被更改。当文件更改时,它会更改哈希值,因此会找到新的更新文件(因为 URL 在技术上已更改)。

同样,没有什么能真正完全证明可以防止客户端缓存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 2017-08-26
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    相关资源
    最近更新 更多