php - 强制浏览器缓存 .php

标签 php http-headers

我需要浏览器缓存一个大的、主要是静态的 .php 文件。我通过 ajax 打开它并想将它添加到当前页面。

经过一番研究后发现this

$seconds_to_cache = 3600;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");

这适用于 IE,但不适用于 chrome 和 firefox。

这是请求

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control   max-age=0
Connection  keep-alive
Content-Type    application/x-www-form-urlencoded
Cookie  PHPSESSID=5dkvr42f4it8pnnnqpesj6l413
Host    localhost
Referer http://localhost/mifa/Suche.php
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1
charset utf-8

这里是响应头

Cache-Control   max-age=3600
Connection  Keep-Alive
Content-Type    text/html
Date    Thu, 05 Jul 2012 15:28:22 GMT
Expires Thu, 05 Jul 2012 16:28:22 GMT
Keep-Alive  timeout=5, max=91
Pragma  cache
Server  Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
Transfer-Encoding   chunked
X-Powered-By    PHP/5.3.8

我需要改变什么?

编辑

显然,只有 IE 没有在请求中附加 Cache-Control max-age=0。

这里是请求的JS函数

url = "includes/Orte.php";
obj.onreadystatechange = rState;
obj.open("GET", url, true);
obj.setRequestHeader("Pragma", "");
obj.setRequestHeader("Cache-Control", "");
obj.setRequestHeader("charset", "utf-8");
obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
obj.setRequestHeader("Connection", "close");
obj.send();

function rState(){
    if(obj.readyState == 4){
        if (obj.status == 200){
            //alert("Response Text Ajax:\n" + obj.responseText + "\nEnd Response Text");
        }
    }
}

最佳答案

请求中的 Cache-Control: max-age=0 header 表示您要求浏览器刷新页面,因此他只是忽略了缓存。

访问页面而不点击刷新(例如聚焦地址栏并点击回车)以避免这种情况。

此外,如果页面位于 HTTPS URL 上,您可能必须将 public 添加到 Cache-Control header ,否则某些浏览器不会缓存它。

关于php - 强制浏览器缓存 .php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11347624/

相关文章:

silverlight - SharePoint 2010/IIS 7.5 字节范围请求响应整个文件

http - 为什么 HTTP 响应头中的 "Connection"拼写错误?

javascript - 使用 .each 制定选项时无法使用 $.post 请求读取 $_POST 值

php - PDO 不绑定(bind)变量

javascript - 区分页面的第一次下载和同一页面的所有后续下载

Groovy HTTPBuilder 获取上次修改日期时间

asp.net - 为什么当我使用 Server.Transfer() 时没有创建 HTTP header ?

php - Mysql 正确比较2个日期时间?

php - 条件 PDO bindParam 更新

php - 在 mySQL 结果中,如何访问字段值以根据其他字段值选择变量