Tech Musings

Sunday, November 27, 2005

http headers and "no-cache" for dynamic pages

to refresh or force a page to not cache, send a header with php page similar to this:

<-php header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past -?>
<-DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"->

This header needs to come before any html tags in the source code. This worked on my change_password.php page for the Pt. Loma e-portfolio page. When a user originally updated his/her password or name, the updated data did not display immediately if he or she clicked again on the "change" link to see the changes. They would have needed to know how to manually refresh the pop-up page to see the updated changes. This was not desirable. After including the no-cache header, the updated changes were seen in the change_password.php page without the need for users to manually refresh.

>>>>ADDED 4/7/06
There are times when you want to use the browser's cache. For example, if you create a form that includes server-side validation like the upload.php on teacher_exchange, and a user submits information in the form that generates an alert (e.g. the file's filename is already on the server), it is good to cache the information in the form so the user doesn't have to retype everything after hitting the browser's back button.

In this case I used the following:

header("Cache-Control: private");

This works pretty well except in Firefox < 1.5. Here is an article on the subject:


Post a Comment

Links to this post:

Create a Link

<< Home