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

时间:2012-11-30 06:26:12

标签: php http-headers browser-cache cache-control

我有一个在云服务器上运行的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)">

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

6 个答案:

答案 0 :(得分:243)

试试这个

<?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");
?>

答案 1 :(得分:27)

此处,如果您想通过HTML控制它:请执行以下操作选项1:

<meta http-equiv="expires" content="Sun, 01 Jan 2014 00:00:00 GMT"/>
<meta http-equiv="pragma" content="no-cache" />

如果您想通过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');

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

答案 2 :(得分:9)

你可以试试这个:

    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");

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

答案 3 :(得分:5)

我在缓存我的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') : ""; ?>">

修改

或者更好的两者组合:

<?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(); ?>">

答案 4 :(得分:3)

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

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

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

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

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

答案 5 :(得分:-1)

您还可以对缓存文件使用查询字符串。它不会影响您的样式和 js 文件的行为。

例如:

example.com/mystyle.css -> 
example.com/mystyle.css?<?php echo random(1000, 5000); ?>