In working with Lynda (who did MOST of the work! LOL!) on that subscribe to comments script, one of the problems we ran into was with the back button. If after submitting a comment, you hit the back button (which was scary HOW MANY people use it when navigating) it would resubmit the comment and resubmit the email. This has since been fixed, but thought I'd post a little bit about what I learned during this process about back button navigating…
We tried to set a session cookie, didn't work.
We tried to use a variable that was changed once everything was sent, didn't work.
We tried just about everything we could think of.
The only thing that DID work was putting in this line of javascript on the page which basically says – if there's a page to forward this person to – send them there now.
<script language="JavaScript">
<!–
window.history.forward(1);
//–>
</script>
Now what I find most surprising about the fact that this works, is that it's *javascript* which means that it loads AFTER all the php stuff is done… on the CLIENT side. However, without it, the emails get sent out – but that is PHP which is supposedly done BEFORE the javascript is done – because PHP is done on the server – and then it sends you the html page after it's done it's PHP magic, then any javascript gets done on your machine locally.
That being the case… why would this solution work? You'd think that when you hit the back button, since the page is in the client's side cache – that NO php would process again (unless you RELOADED the page – which basically asks for the page from the server again… and the server would process the PHP before sending you the page… etc. etc..) because… PHP is all server stuff.. in fact, you can't even see the PHP code locally…
The only conclusion that I can come up with is this (and please note that I MAY be wrong – I'm just trying to draw a conclusion based on the facts):
When you hit the back button:
Server side stuff IS processed again, BUT variables, cookies, etc. have the same VALUE as they did when the page was first loaded.
Why I think this is true: Because we tried setting a cookie at the time the form was submitted. The value of this cookie was changed AFTER all the stuff had processed. However, at the time the page was originally loaded – the value of the cookie gave the green light for the mail to be sent. Even though that cookie's value had changed… if you hit the back button, it remembered the value of the cookie as it was when the page was first loaded, which gave the green light for everything… and would send the mail a second time. This also holds true for changing values of PHP specific variables…
Since the page is in cache – it will remember any javascript things/processes.
Why I think this is true: Because our solution works. I DON"T think that it processes javascript and then php… but it does seem that way…