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 Reply

Your email address will not be published.