Remember Form Data on Back Button

Sometimes it's handy to have the browser cache an entire page including anything that is entered into a form. What if a user has filled out an entire form but accidently clicks refresh or another link, and when they click back the form is empty. Anoying! This also works if you submit to an error checking page, and you want the browser to remember the form data instead of writing something in PHP. How to do this is below.

The easiest way to tell the browser that they can cache the page is by doing this:

header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime("+2 day")));

Only do this on pages that are not dynamic, as the browser will keep this page in cache for 2 days.

Put the above code AFTER any session_start(); if you are using sessions.

And that's how to control cache when back button clicked.

Leave a Comment

Yes, send me an email when a new comment is posted.

The avatars shown next to comments are Gravatars. Click here to get a Gravatar account for free and any other site that supports it will show your avatar too!