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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13640109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:49:49  来源:igfitidea点击:

How to prevent Browser cache for php site

phphttp-headersbrowser-cachecache-control

提问by ArrayOutOfBound

I have a php site running in cloud server.When ever i add new files css, js or images the browser is loading the same old js, css and image files stored in cache.

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

My site has a doctype and meta tag as below

我的网站有一个文档类型和元标记,如下所示

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

Because of the above doctype and meta code am i loading the same files cached in browser instead of new one

由于上述文档类型和元代码,我加载浏览器中缓存的相同文件而不是新文件

回答by Codesen

try this

尝试这个

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

回答by Ritesh Aryal

Here, if you want to control it through HTML: do like below Option 1:

在这里,如果你想通过 HTML 来控制它:像下面的选项 1 一样:

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

And if you want to control it through PHP: do it like below Option 2:

如果你想通过 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 Option 2IS ALWAYS BETTER in order to avoid proxy based caching issue.

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

回答by Aakanksha

You can try this:

你可以试试这个:

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

Hopefully it will help prevent Cache, if any!

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

回答by Lukas

I had problem with caching my css files. Setting headers in PHP didn't help me (perhaps because the headers would need to be set in the stylesheet file instead of the page linking to it?).

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

I found the solution on this page: https://css-tricks.com/can-we-prevent-css-caching/

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

The solution:

解决方案:

Append timestamp as the query part of the URI for the linked file.
(Can be used for css, js, images etc.)

附加时间戳作为链接文件的 URI 的查询部分。
(可用于 css、js、图像等)

For development:

用于开发:

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

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

For production (where caching is mostly a good thing):

对于生产(缓存主要是一件好事):

<link rel="stylesheet" type="text/css" href="style.css?version=3.2">
(and rewrite manually when it is required)

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

Or combination of these two:

或者这两者的组合:

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

EDIT:

编辑:

Or prettier combination of those two:

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

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

回答by ismavolk

Prevent browser cache is not a good idea depending on the case. Looking for a solution I found solutions like this:

根据情况阻止浏览器缓存不是一个好主意。寻找解决方案我找到了这样的解决方案:

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

the problem here is that if the file is overwritten during an update on the server, which is my scenario, the cache is ignored because timestamp is modified even the content of the file is the same.

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

I use this solution to force browser to download assets only if its content is modified:

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

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