fire
fire

Reputation: 21531

Preventing cache of inline PDF

I am trying to prevent caching of an inline PDF file using the following code (adapted from CodeIgniter's download helper):

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();

Can anyone spot if these headers might cause any issues in IE or any browser, such as conflicts?

Upvotes: 2

Views: 7008

Answers (2)

Murukesh
Murukesh

Reputation: 600

The CodeIgniter download helper link you provided is not having the code where you probably copied the code snippet in the question. I am not sure why you need separate set of headers for IE. But it looks like for header parameter Cache-Control, you have to set value no-cache. must-revalidate is for client app to cache the file, but validate it before displaying/using. This is one link i found which should be working fine for PHP as well: http://blog.serendeputy.com/posts/how-to-prevent-browsers-from-caching-a-page-in-rails/

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359786

To prevent caching of dynamic content, all I use is this (and I haven't noticed any caching issues yet):

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

This is (hopefully) the PHP equivalent of what my Java apps use - apologies for any translation errors.

Upvotes: 7

Related Questions