我正在编写一个修改自己内容的脚本,我很难阻止它自己缓存而不显示更新的页面......
我已经尝试了一切我能想到的...... 我在PHP代码中添加了这些头文件:
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
header('Expires: 0'); // Proxies.
我在HTML代码中添加了这些元标记:
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
我甚至在其中添加了一个.htaccess文件:
Options -Indexes
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
...然而浏览器一直在缓存......我还能做什么吗?
由于
答案 0 :(得分:0)
使用.htaccess很好,如果你有权访问httpd.conf就更好了。无论哪种方式,这是正确的。但是,您也可以基于每个脚本操作浏览器缓存,如下所示:
<?php
header("Last-Modified: " . <whatever date you want refreshing>));
header("Expires: " . <whatever date you want refreshing>));
header("Cache-Control: max-age=<whatever date ..>);
?>
<!doctype html>
<html lang="en">
<head>
...
答案 1 :(得分:0)
更改文件后是否清除了缓存?我知道我之前曾与之搏斗,这是由于一个简单的因素导致我在进行更改后没有清除缓存。无论如何,你把所有东西都放在了我自己的顶端。
答案 2 :(得分:0)
大多数网络浏览器会根据Last-Modified
缓存一些内容。有一些技巧可以告诉浏览器更新可能缓存的文件:
使用touch
可以更新文件的时间戳:
<?php
$time = time() + 60; // Time Now + One Minute
$file = '/path/to/your/file/';
touch($file, $time);
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
&&
(strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($file))) {
// Send Result To Client
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($file)).' GMT',
true, 304);
exit;
}
?>
这将在页面加载时随时更新文件的时间戳+1分钟 - 然后将其与客户端可能在本地缓存的版本进行比较。如果您不想更新文件时间戳,也可以使用$time
变量而不是filemtime($file)
。