Forums

PHP

This topic is locked

session_start() and history.back(-1)

Posted 16 Jan 2002 19:27:28
1
has voted
16 Jan 2002 19:27:28 Jose Ortega posted:
Hallo!

I have the following problem with a form-mailer script that I am working on:
After the user hits the submit button I validate the input, if an error occurs a message is displayed and the user can return to the filled out form with his browser's back button. So far so good...

Now I have started to use sessions -- session_start() -- in order to pass on variables. The problem is that whenever I am using the back button with session_start() I return to a blank form. Without session_start() everything works just fine and the form data is preserved.

Does anybody know how to work around that problem?

Thanks,
Jose

Replies

Replied 16 Jan 2002 22:49:14
16 Jan 2002 22:49:14 Tim Green replied:
The best way to do this is for everything to be in the same page (so you don't have to use the Back button at all!).

Inside your form insert a hidden field. Let's call it 'formseen' and set the value to 'true'...

Now at the very beginning of your page insert something like the following:-

<?php
if (isset($formSeen)) { // Form has been submitted
// enter your processing here
if ($complete) {
header("Location: formcomplete.php";
} else {
echo "You missed a field";
}
}
?>

That gives you the basic structure, and removes the need for putting everything in a session variable. If you need to pass the values of the form to the next page, then change:-

header("Location: formcomplete.php";

to:-

$url = "formcomplete.php?".explode("&",$HTTP_POST_VARS);
header("Location: $url";

This will pass the variables over to the next page within the URL so you can access them.

Hope this helps <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>
Replied 17 Jan 2002 00:44:55
17 Jan 2002 00:44:55 Jose Ortega replied:
Hallo again!

First of all thanks for your reply! I am already using only one page, the thing is that I want to pass on a variable to the same page after it's been reloaded.
After the user hits the submit button all data in the input fields is gone, so if something was wrong with the data the user would have to reenter everything. Therefore I do allow caching which enables the user to go back one step in his browser's history (back-button) and have the filled out form again.
As soon as the data is processed and sent, however, there is no need for the user to go back to the inserted information any more. So when the mail was sent successfully I disable caching and reload the page:

$ok=mail(...);
if ($ok==1){
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT";
header("Last-Modified: " . gmdate("D, d M Y H:i:s" . " GMT";
header("Cache-Control: no-store, no-cache, must-revalidate";
header("Pragma: no-cache";
header("Location: $PHP_SELF";
}

For displaying the appropriate messages ("Your email was sent..." I need to pass on $ok so that after(!) reloading the page I can display the message. It's working fine with cookies -- the problem is when cookies are disabled in somebody's browser.
Therefore I would like to pass on $ok as a session variable. To be able to use session variables, however, I have to put session_start() on top of my page and as soon as I do that I CAN NEVER USE THE BACK-BUTTON of my browser without receiving a message that tells me to refresh.

Until the mail was actually sent, however, I would like that the user has the chance to use his back button so that he doesn't have to reenter everything.

I understand that I could use a hidden form field. But is there also a way with sessions? Does session_start() disable caching or why can't I go back in my browser's history?

Maybe you (or somebody else) could explain that to me.

Thanks for all your help!

Jose
Replied 19 Jan 2002 02:38:54
19 Jan 2002 02:38:54 Tim Green replied:
Using a session for this is a little overkill, and you'll run into potential problems too, as unless you're using full db-enabled sessions the session creation process creates a cookie to reference the session id....

However, there is a simple solution. I see you are using a header redirect... you could pass your variable via that. So instead of:-

header("Location: $PHP_SELF";

you would have :-

$url = $PHP_SELF."?ok=".$ok;
header("Location: $url";

This would pass the variable value you need back to the page.....

Hope this helps.

Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>
Replied 19 Jan 2002 17:37:44
19 Jan 2002 17:37:44 Jose Ortega replied:
Dear Tim Green!

First of all I want to thank you for your helpful comments and all the work you put into answering my questions.

I tried out your suggestion with passing on the variable via the url-redirect. While it is a very elegant way to pass on variables it still doesn't seem to solve my caching problem.

After the mail was successfully sent I turn off caching via headers as shown in my previous post. The problem now is that the url with which I reload the page changes from $PHP_SELF to $PHP_SELF?ok=$ok and that the browser seems to treat this like a different page. When I reload the page with $PHP_SELF and caching turned off the browser "overwrites" the cache and there is nothing I could go back to with the back button, but when calling the page with $PHP_SELF?ok=$ok it seems like the browser is loading a new instance of the page and doesn't "overwrite" the cached information of the form - using the back button I return to the completed form which I initially wanted to prevent.
Since the only reason why I reload the page is to make it impossible for users to go back and resend the form I might as well not bother reloading the page and passing on that variable.

But maybe I am following a dead end anyways and the problem I am trying to solve should be tackled in a completely different way. My goal was to write a formmailer that allows the user to return to the information he submitted as long as some error was produced and the mail wasn't successfully sent. After that it should be impossible to resend the same form. I already have a floodprotection feature implemented that is based on cookies, but I would prefer a solution that also works on browsers that have cookies (and java) disabled. Can this be accomplished at all or is there a good example script I should have a look at? I already had a look at many different scripts but none of them really seemed to be able to stop all browser configurations from resending an already submitted form.

Sorry for bothering you with this again!

Many thanks,

Jose
Replied 20 Jan 2002 02:00:43
20 Jan 2002 02:00:43 Tim Green replied:
OK, Jose. I think I better understand your objective.

There is another, less elegant way to do this. Once the page has been submitted, and processed successfully you could dynamically write some javascript to the page, so that anyone clicking on the 'Back' button will only get the current page.

If none of the previous options have worked for you, then unfortunately all I can think of now is the above, but I hope it is of some help.

All the best

Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>

Reply to this topic