【问题标题】:Preventing cache of inline PDF防止缓存内联 PDF
【发布时间】:2011-06-10 12:52:01
【问题描述】:

我正在尝试使用以下代码(改编自 CodeIgniter 的 download helper)来防止缓存内联 PDF 文件:

if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
    header('Content-Type: application/pdf');
    header('Content-Disposition: inline; filename="'.$this->folder_name($report['Report_Name']).'.pdf"');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Content-Transfer-Encoding: binary');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file . ".pdf"));
}
else {
    header('Content-Type: application/pdf');
    header('Content-Disposition: inline; filename="'.$this->folder_name($report['Report_Name']).'.pdf"');
    header('Content-Transfer-Encoding: binary');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
    header('Pragma: no-cache');
    header('Content-Length: ' . filesize($file . ".pdf"));
}

readfile($file . ".pdf");
exit();

谁能发现这些标头是否会导致 IE 或任何浏览器出现任何问题,例如冲突?

【问题讨论】:

    标签: php header http-headers


    【解决方案1】:

    为了防止缓存动态内容,我只使用了这个(我还没有注意到任何缓存问题):

    header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1
    header('Pragma: no-cache'); // HTTP 1.0
    header('Expires: 0'); // Proxies
    

    这(希望)是我的 Java 应用程序使用的 PHP 等价物 - 对任何翻译错误表示歉意。

    【讨论】:

    • 谢谢,似乎从来没有一种“官方”方式可以为所有浏览器以正确的顺序发送正确的标头
    【解决方案2】:

    您提供的 CodeIgniter 下载帮助程序链接没有您可能在问题中复制代码 sn-p 的代码。我不确定为什么您需要单独的 IE 标头集。但看起来对于头参数 Cache-Control,您必须设置值 no-cachemust-revalidate 用于客户端应用程序缓存文件,但在显示/使用之前对其进行验证。这是我发现的一个链接,它也应该适用于 PHP:http://blog.serendeputy.com/posts/how-to-prevent-browsers-from-caching-a-page-in-rails/

    【讨论】:

      猜你喜欢
      • 2015-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多