如何防止php站点的浏览器缓存

Arr*_*und 106 php cache-control browser-cache http-headers

我有一个在云服务器上运行的php站点.当我添加新文件css,js或images时,浏览器正在加载存储在缓存中的相同的旧js,css和图像文件.

我的网站有一个doctype和meta标签,如下所示

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Page-Enter" content="blendTrans(Duration=1.0)">
  <meta http-equiv="Page-Exit" content="blendTrans(Duration=1.0)">
  <meta http-equiv="Site-Enter" content="blendTrans(Duration=1.0)">
  <meta http-equiv="Site-Exit" content="blendTrans(Duration=1.0)">
Run Code Online (Sandbox Code Playgroud)

由于上面的doctype和元代码,我加载缓存在浏览器中的相同文件而不是新文件

Cod*_*sen 257

试试这个

<?php

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
Run Code Online (Sandbox Code Playgroud)

  • 除了"max-age = 0"之外,那些是PHP发送的标题,而没有在我的安装中指定上面的内容.似乎PHP试图默认阻止浏览器缓存... (6认同)
  • 注意:如果之后使用`session_start()`,它将用`Cache-Control:private,max-age = 10800,pre-check = 10800`覆盖你的头,因为180分钟是`session.cache_expire`的默认值.如果你不能避免启动会话,但你需要禁用缓存使用`session_cache_limiter('private'); session_cache_expire(0);`. (5认同)
  • 请记住,这不能嵌入到html中; 这应该在页面的最顶层. (3认同)
  • @thdoan [`header`](https://www.php.net/manual/en/function.header.php) 函数的第二个参数是 *replace* 的 **boolean** 。可选的replace参数指示标头是否应替换先前的类似标头,或添加相同类型的第二个标头。 (2认同)

Rit*_*yal 31

在这里,如果您想通过HTML控制它:请执行以下选项1:

<meta http-equiv="expires" content="Sun, 01 Jan 2014 00:00:00 GMT"/>
<meta http-equiv="pragma" content="no-cache" />
Run Code Online (Sandbox Code Playgroud)

如果你想通过PHP控制它:如下面的选项2:

header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
Run Code Online (Sandbox Code Playgroud)

选项2总是更好,以避免基于代理的缓存问题.


小智 10

你可以试试这个:

    header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    header("Connection: close");
Run Code Online (Sandbox Code Playgroud)

希望它有助于防止Cache,如果有的话!

  • 只是第一行应该足够完美.第5行实际上是完全错误的,并且在服务器响应中无关(它是请求标头).第六行将不会有任何影响.我可以继续...... (3认同)

Luk*_*kas 6

我在缓存CSS文件时遇到问题。在PHP中设置标题对我没有帮助(也许是因为需要在样式表文件中设置标题,而不是页面链接到它吗?)。

我在此页面上找到了解决方案:https : //css-tricks.com/can-we-prevent-css-caching/

解决方案:

将时间戳记作为链接文件的URI的查询部分。
(可用于CSS,JS,图像等)

开发:

<link rel="stylesheet" href="style.css?<?php echo date('Y-m-d_H:i:s'); ?>">

对于生产(在大多数情况下,缓存是一件好事):

<link rel="stylesheet" type="text/css" href="style.css?version=3.2">
(并在需要时手动重写)

或这两个的组合:

<?php
    define( "DEBUGGING", true ); // or false in production enviroment
?>
<!-- ... -->
<link rel="stylesheet" type="text/css" href="style.css?version=3.2<?php echo (DEBUGGING) ? date('_Y-m-d_H:i:s') : ""; ?>">
Run Code Online (Sandbox Code Playgroud)

编辑:

或这两个的更漂亮的组合:

<?php
    // Init
    define( "DEBUGGING", true ); // or false in production enviroment
    // Functions
    function get_cache_prevent_string( $always = false ) {
        return (DEBUGGING || $always) ? date('_Y-m-d_H:i:s') : "";
    }
?>
<!-- ... -->
<link rel="stylesheet" type="text/css" href="style.css?version=3.2<?php echo get_cache_prevent_string(); ?>">
Run Code Online (Sandbox Code Playgroud)


小智 5

视情况而定,防止浏览器缓存不是一个好主意。在寻找解决方案时,我发现了这样的解决方案:

<link rel="stylesheet" type="text/css" href="meu.css?v=<?=filemtime($file);?>">
Run Code Online (Sandbox Code Playgroud)

这里的问题是,如果在服务器上的更新过程中文件被覆盖(这就是我的情况),则缓存将被忽略,因为即使文件内容相同,时间戳也会被修改。

我使用此解决方案强制浏览器仅在其内容被修改时才下载资产:

<link rel="stylesheet" type="text/css" href="meu.css?v=<?=hash_file('md5', $file);?>">
Run Code Online (Sandbox Code Playgroud)

  • @Dalin 在你为 Gentoo Ricer(一个因过度从源代码编译和架构调整而“快速”而闻名的 Linux 发行版)哭泣之前,我会记录一个“stat”调用。没有文件系统缓存,16ns,最高?带缓存,可靠 &lt; 8ns。纳秒。在我的系统上,MD5 可以不眨眼地处理 754 MiB/s。(`openssl speed md5`)综合起来,100KB CSS 文件的综合额外开销为…129μs(微秒,0.1295ms)+ 8ns(对最终数字没有多大影响)= 129μs。 (3认同)
  • 经过进一步考虑,令我震惊的是,唯一的“正确”答案(具有最低的维护负担,最准确/可靠的行为)既是投票最少的,又以如此脆弱和不切实际的理由在一条评论中被驳回。 (2认同)