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)
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,如果有的话!
我在缓存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)