views:

793

answers:

1

Hi all
Why are my session variables being dropped here?

I submit a form using the submit button and create session variables of the values to allow them to be carried to other pages. All's fine. If I now move back and forward between the form and form handler using the browser buttons (as some user sometime will probably do) Firefox and Opera recalls the session variables and can populate the form handler. IE and Safari however don't. What can I do about it?

Firefox would demonstrate the problem if I hadn't inserted the following in the header file earlier this morning:

header("Cache-Control: no-cache");
header("Expires: -1");

I've read that the second header line - header("Expires: -1"); - should also be the fix for IE but that seems not to be the case. IE8 / Safari still show the problem. Opera doesn't need this at all. If this is a reload problem what do I need to add to the header to force IE and Safari to reload?

Thanks

+1  A: 

Your session variables are not lost. Rather, what's happening is that, when going back, none of the browsers will reload the form from your server (with the defaults filled in from the session variables.) Some browsers, however, will be nice about it and remember themselves what values the form fields last had when that particular form was last submitted, and fill them in for you. IE will not.

In any case, it is good practice for forms to be submitted via POST, and for the server to respond to the POST by redirecting (HTTP 302) to the next page, instead of rendering the next page directly. By issuing the redirect, any attempt by the user to go back will ask him/her to confirm whether they really want the form resubmitted for a second time, which could be a bigger problem than just not having some pre-filled values on the form. I'm guessing here what your real inentions are, but if you really want the user to be able to submit multiple forms in a row (repetitive task), simply render the form again on the next page, after the redirect.

Cheers, V.

vladr