防止内联PDF的缓存

时间:2011-01-17 15:32:18

标签: php header http-headers

我正在尝试使用以下代码(改编自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或任何浏览器出现任何问题,例如冲突?

2 个答案:

答案 0 :(得分:7)

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

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相当 - 对任何翻译错误表示道歉。

答案 1 :(得分:0)

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