472,352 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

Clearing POST DATA for Browser Refresh

PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise,
click Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed,
the page is reloaded without this message being displayed.

Thanks in advance.
Feb 14 '06 #1
15 32664
tmax wrote:
PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise,
click Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed,
the page is reloaded without this message being displayed.

Thanks in advance.

Don't redirect to the form; redirect to a summary page without the form
or make your form page sensitive to whether the $_POST data is available
and do not have it emit a <form> if the $_POST data is available.

I prefer the first method over the second.

-david-

Feb 14 '06 #2
d
"tmax" <tm**@comcast.net> wrote in message
news:82***************************@PGTV.COM...
PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise, click
Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed, the
page is reloaded without this message being displayed.

Thanks in advance.


In the script to which the form submits, store the $_POST data somewhere
else, say a session. Then, use this:

header("Location: http://host/path/to/script.php");
exit();

to bounce the user to another page. Most browsers know to not re-submit the
post data to this new page. The new script can even be the originating
one - as long as you use the location: header, you'll be fine. You can then
access the submitted data wherever you stored it.

PS. if you're using a session to store the data, call session_write_close()
before the header() call, otherwise nasty things happen on some browsers.

dave
Feb 14 '06 #3

d wrote:
"tmax" <tm**@comcast.net> wrote in message
news:82***************************@PGTV.COM...
PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise, click
Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed, the
page is reloaded without this message being displayed.

Thanks in advance.


In the script to which the form submits, store the $_POST data somewhere
else, say a session. Then, use this:

header("Location: http://host/path/to/script.php");
exit();

to bounce the user to another page. Most browsers know to not re-submit the
post data to this new page. The new script can even be the originating
one - as long as you use the location: header, you'll be fine. You can then
access the submitted data wherever you stored it.

PS. if you're using a session to store the data, call session_write_close()
before the header() call, otherwise nasty things happen on some browsers.

dave


Just curious, but is there / would you recommend a good alternative for
the Sessions?

Frizzle.

Feb 14 '06 #4
Well, I've tried everything that has been suggested so far - thanks for
everyone's input.

However, let me restate the problems differently as I believe it wasn't
correct to begin with.

I have a file: index.php

index.php contains a form, which when submitted for processing, calls
itself using <form method="POST" action="{$_SERVER['PHP_SELF']}">

When the form is submitted and index.php is reevaluated, I have the
following IF statement: (FYI: 'submit' is the name of the submit button.)
------------
IF ( !isset($_POST['submit']) ) {
// The form has NOT been SUBMITTED - display the form

echo <<<htmloutput
<form method="POST" action="{$_SERVER['PHP_SELF']}">
...
</form>
htmloutput;

} ELSE {
// The form was SUBMITTED - process the form

process the form data, etc. and print in PLACE of the form the
following:

echo "Information submitted... Thank you.";
}
-------------

I understand I can redirect to a (different) summary page, but all I
want to do is replace the form with the summary message. The problem
comes into being when, after the summary message is displayed, the user
goes to refresh the browser. The browser is still caching the form data
for resubmission. I've tried things like: unset($_POST) or
unset($_POST['submit']) (and all the other form fields) but that has no
affect.

Any other suggestions?

Thanks in advance.

d wrote:
"tmax" <tm**@comcast.net> wrote in message
news:82***************************@PGTV.COM...
PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise, click
Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed, the
page is reloaded without this message being displayed.

Thanks in advance.

In the script to which the form submits, store the $_POST data somewhere
else, say a session. Then, use this:

header("Location: http://host/path/to/script.php");
exit();

to bounce the user to another page. Most browsers know to not re-submit the
post data to this new page. The new script can even be the originating
one - as long as you use the location: header, you'll be fine. You can then
access the submitted data wherever you stored it.

PS. if you're using a session to store the data, call session_write_close()
before the header() call, otherwise nasty things happen on some browsers.

dave

Feb 14 '06 #5
I have to ask, why would you assume your users would refresh that page
rather than following a link somewhere else?

Feb 14 '06 #6
I have to ask, why would you assume your users would refresh that page
rather than following a link somewhere else?

Feb 14 '06 #7
I don't think there is another solution than storing state information
somehwere.
I would use the following method:

1. Before displaying the form, generate some unique number
2. add this number in a hidden field in the form.
3. When the user submits, mark the number as 'used'
4. When the user submits again, ignore the data, because the number
has already been 'used' up.

The simplest way to do this is to create a counter, either in a file on disk
or in
a database. Before you send the form to the client, increment the counter
and send
the counter value in a hidden field in the form.

If you save the form data in a database table, add a 'counter' field to the
table.
When the form is first submitted, a record with the counter value sent with
the form should not exist. Save data and counter.
If the form is submitted for a second or third time, the counter is already
present in
your table, so you should ignore the data in the post.

Using a session cookie is, I think, less work.
"tmax" <tm**@comcast.net> wrote in message
news:82***************************@PGTV.COM...
PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise, click
Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed, the
page is reloaded without this message being displayed.

Thanks in advance.

Feb 14 '06 #8

True, they could instead following some other link and decide not to
refresh the page. However, I would like the page to function such that
refreshing the page simply re-displays a new blank form (after all,
refreshing a page is conceptually akin to resetting.)

I started off on this path thinking that this format should be
relatively easy to code with one if-else statement. So far, it has been
easy to code (the form and summary message work great, with very little
coding). I keep thinking that there is some PHP mechanism that isn't
being used properly here (due to my PHP inexperience.)

Any suggestions?

P-Rage wrote:
I have to ask, why would you assume your users would refresh that page
rather than following a link somewhere else?

Feb 14 '06 #9
d
"frizzle" <ph********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

d wrote:
"tmax" <tm**@comcast.net> wrote in message
news:82***************************@PGTV.COM...
> PHP Pros:
>
> I have a simple html form that submits data to a php script, which
> processes it, and then redisplays the same page, but with a "thank you"
> message in place of the html form. This is all working fine. However,
> when refresh the browser, I get the following message displayed:
>
> "The page you are trying to view contains POSTDATA. If you resend the
> data, any action the form carried out (such as as search or online
> purchse) will be repeated. To resend the data, click OK. Otherwise,
> click
> Cancel."
>
> Obviously I don't want my users to resend the data to me.
>
> What do I need to do code-wise so that when the browser is refreshed,
> the
> page is reloaded without this message being displayed.
>
> Thanks in advance.
In the script to which the form submits, store the $_POST data somewhere
else, say a session. Then, use this:

header("Location: http://host/path/to/script.php");
exit();

to bounce the user to another page. Most browsers know to not re-submit
the
post data to this new page. The new script can even be the originating
one - as long as you use the location: header, you'll be fine. You can
then
access the submitted data wherever you stored it.

PS. if you're using a session to store the data, call
session_write_close()
before the header() call, otherwise nasty things happen on some browsers.

dave


Just curious, but is there / would you recommend a good alternative for
the Sessions?


Databases or files spring to mind :)
Frizzle.

Feb 14 '06 #10
d
"tmax" <tm**@comcast.net> wrote in message
news:1f***************************@PGTV.COM...
Well, I've tried everything that has been suggested so far - thanks for
everyone's input.

However, let me restate the problems differently as I believe it wasn't
correct to begin with.


I understand completely what you're asking :)

Try this code:

<?

session_start();

if (isset($_POST["data"])) {
$_SESSION["data"]=$_POST["data"];
session_write_close();
header("Location: ".$_SERVER["SCRIPT_URI"]);
exit();
}

if (isset($_SESSION["data"])) {
?>
<html>
<head><title>Thanks!</title></head>
<body>
Thank you for submitting your data:<br>
<?=$_SESSION["data"];?>
</body>
</html>
<?
} else {
?>
<html>
<head><title>Please Submit</title></head>
<body>
Use the form to submit your data:<br>
<form method="post">
<input type="text" name="data"><br>
<input type="submit" value="Go!">
</form>
</body>
</html>
<?
}
?>

It will show a form, and take that posted data and store it in a session.
Then, the script re-directs you back to itself, and that redirection stops
your browser wanting to resubmit the data when refreshed. The session is
then checked, and if the data is present, a thank-you note is displayed. If
not, it then shows the initial form.

dave
Feb 14 '06 #11
d
"tmax" <tm**@comcast.net> wrote in message
news:a3***************************@PGTV.COM...

True, they could instead following some other link and decide not to
refresh the page. However, I would like the page to function such that
refreshing the page simply re-displays a new blank form (after all,
refreshing a page is conceptually akin to resetting.)

I started off on this path thinking that this format should be relatively
easy to code with one if-else statement. So far, it has been easy to code
(the form and summary message work great, with very little coding). I
keep thinking that there is some PHP mechanism that isn't being used
properly here (due to my PHP inexperience.)

Any suggestions?
The header("Location: ") command :) See my other post... ;)
P-Rage wrote:
I have to ask, why would you assume your users would refresh that page
rather than following a link somewhere else?

Feb 14 '06 #12
Thank You Dave!!!

This code works - I used ob_start() to get around some header() errors,
but after implementing that, my form and page works exactly as I
intended it to. Learned quite a bit from all of this too. Thanks again!

Travis
d wrote:
"tmax" <tm**@comcast.net> wrote in message
news:1f***************************@PGTV.COM...
Well, I've tried everything that has been suggested so far - thanks for
everyone's input.

However, let me restate the problems differently as I believe it wasn't
correct to begin with.

I understand completely what you're asking :)

Try this code:

<?

session_start();

if (isset($_POST["data"])) {
$_SESSION["data"]=$_POST["data"];
session_write_close();
header("Location: ".$_SERVER["SCRIPT_URI"]);
exit();
}

if (isset($_SESSION["data"])) {
?>
<html>
<head><title>Thanks!</title></head>
<body>
Thank you for submitting your data:<br>
<?=$_SESSION["data"];?>
</body>
</html>
<?
} else {
?>
<html>
<head><title>Please Submit</title></head>
<body>
Use the form to submit your data:<br>
<form method="post">
<input type="text" name="data"><br>
<input type="submit" value="Go!">
</form>
</body>
</html>
<?
}
?>

It will show a form, and take that posted data and store it in a session.
Then, the script re-directs you back to itself, and that redirection stops
your browser wanting to resubmit the data when refreshed. The session is
then checked, and if the data is present, a thank-you note is displayed. If
not, it then shows the initial form.

dave

Feb 15 '06 #13
tmax wrote:
Thank You Dave!!!

This code works - I used ob_start() to get around some header() errors,
but after implementing that, my form and page works exactly as I
intended it to. Learned quite a bit from all of this too. Thanks again!

Travis
d wrote:
"tmax" <tm**@comcast.net> wrote in message
news:1f***************************@PGTV.COM...
Well, I've tried everything that has been suggested so far - thanks
for everyone's input.

However, let me restate the problems differently as I believe it
wasn't correct to begin with.


I understand completely what you're asking :)

Try this code:

<?

session_start();

if (isset($_POST["data"])) {
$_SESSION["data"]=$_POST["data"];
session_write_close();
header("Location: ".$_SERVER["SCRIPT_URI"]);
exit();
}

if (isset($_SESSION["data"])) {
?>
<html>
<head><title>Thanks!</title></head>
<body>
Thank you for submitting your data:<br>
<?=$_SESSION["data"];?>
</body>
</html>
<?
} else {
?>
<html>
<head><title>Please Submit</title></head>
<body>
Use the form to submit your data:<br>
<form method="post">
<input type="text" name="data"><br>
<input type="submit" value="Go!">
</form>
</body>
</html>
<?
}
?>

It will show a form, and take that posted data and store it in a
session. Then, the script re-directs you back to itself, and that
redirection stops your browser wanting to resubmit the data when
refreshed. The session is then checked, and if the data is present, a
thank-you note is displayed. If not, it then shows the initial form.

dave


His code should work fine as long as you don't have *anything* before
the first line - including blank lines, spaces and DOCTYPE statements.

You should not need ob_start().

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 15 '06 #14
On 2006-02-14, tmax <tm**@comcast.net> wrote:
PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise,
click Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed,
the page is reloaded without this message being displayed.

Thanks in advance.


I think this will work.
when they post the form issue a redirect
(use a fixed pitch font or the following won't make much sense)

user posts automatic user navigates
form -----> redirect ------> thankyou --------> further
page page

when the user goes back from the "further" page they'll hit the
thankyou page (which was got by the redirect so no data to post)
(back again gets them to the form.)
Bye.
Jasen
Feb 15 '06 #15
P-Rage:
I have to ask, why would you assume your users would refresh that page
rather than following a link somewhere else?


Browsers developed under a misconception of History lists ask you if
you want to resend POST data after, say, you follow a link away from
the page to which the form directed you, and then, for whatever reason,
move Back to that page. By HTTP, if the user has not reconfigured
their browser to refresh stale History items, this should not happen
(sec. 13.13). The solutions put forward in this thread, if serving no
other purpose, work around that bug.

--
Jock

Feb 16 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: brendan | last post by:
Sorry this isnt a cross post .. i just didnt get any help from alt.php. I have a website which utilises post forms for navigation in some areas....
7
by: milkyway | last post by:
Hello to all ;-) I am using the Visual Studio 2005 .NET framework. Basically, I have file that is served up as an HTML file on the client side...
1
by: David P. Donahue | last post by:
I have an ASP .NET website where users submit comments and, depending on whether or not the web service accepting the comments returns an error,...
1
by: Dabbler | last post by:
What effect does the browser refresh button have on a) session variables and b) viewstate? My page loads info based on session variables and it...
4
by: Thomas Eichner | last post by:
Hi, does anybody know a public website which offers a service that displays all data send by a browser (or an app calling the website),...
2
by: pingalkar | last post by:
In my application, on one form i m getting information from user and save this information , after saving this information again we loading that...
8
by: ninuhadida | last post by:
hi there! This is actually my first post over here.. I thought i'd register since most answers to my common problems that i google up turn up to be...
6
by: kiran Vidhate | last post by:
Hi I need small help, What I need is when I write a script to handle my post data on my login page. if login failed and i press refresh key...
4
by: =?Utf-8?B?QXNhZg==?= | last post by:
Hi, How can I prevent from an ASP.NET page to resend all the data again when the user press the Refresh button or F5 on the browser? Thanks in...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.