Tuesday, March 27, 2007

How do I prevent Web browsers caching a page using PHP?

<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Pragma: no-cache");
?>

We can go one step further, using the Cache-Control header that’s supported by HTTP 1.1 capable browsers:

<?php
header("Expires: Mon, 26 Jul 2007 05:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", FALSE);
header("Pragma: no-cache");
?>

How do I prevent Web browsers caching a page using HTML?

The most basic approach to doing this utilizes HTML meta tags:

<meta http-equiv="Expires" content="Mon, 26 Jul 2007 05:00:00 GMT"/>

<meta http-equiv="Pragma" content="no-cache" />

By inserting a past date into the Expires meta tag, we can tell the browser that the cached copy of the page is always out of date. This means the browser should never cache the page.

Monday, March 12, 2007

How to force user to download PDF file?

<?php
// Change the header to allow PDF download
header('Content-type: application/pdf');

// The downloaded file will be called as savethis.pdf
header('Content-Disposition: attachment; filename="savethis.pdf"');

// The source PDF to be downloaded is source.pdf
readfile('source.pdf');
?>