Re: Confusing POST behavior -- doing it twice?



Shelly wrote:

Hi Shelly,

I added a few things that may help.
Being in a hurry (food at home :P), I am not able to look into it in greater
depth.


> Here is the corrected post. I made about 5 errors in typing this post the
> first time that are not in the actual (full) code.
>
> I am confused about what goes on with method POST. Here is an overview of
> a my code, sketching it out:
>
> <?php
> if (isset($_POST['Submit']) && $_POST['Submit']=="Submit") {
> if (isset($_SESSION['Error'])) {
> unset($_SESSION['Error']);
> }

Just a remark: Why do you not always unset the error?
Or is this code in a more complex environment where you want to be able too
keep the custum-errormessage around?
(Not important, just curious)


> if (strcmp($A, "any")) {
> // Verify that a Zip code is numeric and is five digits
> If (!is_numeric($_POST['zip']) || strlen($_POST['zip']) != 5) then
> {
> $_SESSION['Error'] = "an error description";
> header("Location: thisSite.php");

Add
exit 0;

The header you set will just do that: set the header.
The program will run on from there.
Later on you will be able to give it another value (which you actually do).
So you have to force PHP to quit here.


> }
> }
> }
> header("Location: anotherSite.php");

Here you overwrite your previously set header. :-/


> ?>
>
> ..... in the form description I have an
> <?php if (isset(($_SESSION['Error'])) echo $_SESSION['Error']; ?>
>
> (Note: The form is type POST).
>
> So here is the problem:
> 1 - When I start, there is no error message.

good.

> 2 - I deliberately put in a bad zip code and have a value other than "any"
> in the A control and click to submit it.

Ok, and your code catches that just fine. :-)

> 3 - Instead of going back to thisSite.php and displaying the error
> message, it goes to anotherSite.php.

Yes. Because you forgot the exit command.

> 4 - If I then hit the back arrow on the browser, it displays with the
> error message on thisSite.php

Well...
The infamous BACK-button.
I hate that thing.

I have really nothing constructive to tell you on this point.

As far as my experience goes: It is implemented differently on different
browsers.
I have seen the following things (over many years):
Some just fetch the HTML from cache and display that.
Or maybe some even repost (if a form was used to produce the HTML) without
your knowledge.
Others warn you you are viewing possibly out-of-date html.
Or they warn you that you are reposting the form.

Of course you can modify their behavious with extra option in headers
concerning cache-control, document-expires, etc. etc.

Everything is possible and it gives me a headache.

I have no idea how the current mostly used browser behave.
Maybe things improved and they all do the same, but I doubt it.


>
> It seems that
> 1 - on the submit it goes through the logic and sets the error.

Yes.

> 2 - It then must be going through a second time, but this time taking on
> the default setting of "any" for control A and so bypass the logic on the
> zip code and so goes to anotherSite.php.

Because of the missing exit, but I told you that already. :-)

>
> Please help me here.
>

I hope I did.

Regards,
Erwin Moller

> Shelly

.