Can you empty POST data?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Can you empty POST data?

Post by pilau »

At some tutorial was written you can clear POSTDATA by doing $_POST = array();
However, I tried it on different servers and at any case FF said that I still have POST data when I hit "refresh".
So maybe it's not even possible?
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

I think that to clear post data, you would probably want something similar to header("Location: $the_current_page");.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

if you load a page by a form with post data in it then even on that page if you $_POST = array() when you hit refresh it will refresh the page as it was when you first got there, with $_POST data and all.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

POSTDATA is not PHP's $_POST

POSTDATA is sent from the browser to the server. Code on the server will not effect the client's behaviour in that regard.

Firefox after submitting data by POST will request if you want to so again - IF you refresh the page following a form submit. Actually this is one of the reasons I use GET for requests leading to an action (despite what Google Webaccelerator advocates may swear to).

Since you are refreshing, FF takes it you may also want to submit the data again / or not. Either way you get to choose rather than leaving it up a non-sentient browser ;)
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

So the only way to clear POSTDATA is to use header()?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have to jump to a new page request that doesn't use post, that's the only way to get rid of the post data (although it's still stored in the browsers memory)
BruceT
Forum Newbie
Posts: 14
Joined: Sat Aug 27, 2005 10:23 am

Post by BruceT »

Just use

Code: Select all

unset($_POST)
if you want to get rid of all of the data, or

Code: Select all

unset($_POST['foo'])
to get rid of a specific element of the array.


Edit - the browser does still hold the contents in memory though, so if the page is refreshed, it will attempt to resubmit the data. I guess the only way to clear it for good is to load a separate 'clean' page.
Last edited by BruceT on Sat Sep 24, 2005 11:17 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that won't acutally get rid of the data bruce.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Hmm, ok. Understood.
However, a new question had popped in front of my eyes: If I set a new element in the $_POST array, and then submit an HTML form, that is:

Code: Select all

<?php

$_POST['something'] = "string";
print "<form action='this.file' method ='POST'>
<input type='submit'>
</form>";

?>
Will POSTDATA contain the "something" element afterwards?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

pilau wrote: Will POSTDATA contain the "something" element afterwards?
No.
To add a field to $_POST array after resubmit you have to regenerate your form with additional field(s):

Code: Select all

<?php

$_POST['something'] = "string";
print "<form action='this.file' method ='POST'>";
foreach($_POST as $key=>$val) {
   print "<input type='hidden' name='$key' value='$val' />\n";
}
print "
<input type='submit'>
</form>";

?>
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Weirdan wrote:

Code: Select all

<?php

$_POST['something'] = "string";
print "<form action='this.file' method ='POST'>";
foreach($_POST as $key=>$val) {
   print "<input type='hidden' name='$key' value='$val' />\n";
}
print "
<input type='submit'>
</form>";

?>
That should be used if I want to generate the exact form I had before I refreshed the page.
(Which basically I could just copy the source code from the form I made :P)
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

If all you want to archive is that you cannot refresh the post data you can use sessions to disallow the refresh.

You can set a session var on the form page and only allow post data to be shown if that var is correct. After the data is shown change that var. For the cookie disabled you can use sid or tell them to enable cookies to submit the form.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

pilau wrote:
Weirdan wrote:

Code: Select all

<?php

$_POST['something'] = "string";
print "<form action='this.file' method ='POST'>";
foreach($_POST as $key=>$val) {
   print "<input type='hidden' name='$key' value='$val' />\n";
}
print "
<input type='submit'>
</form>";

?>
That should be used if I want to generate the exact form I had before I refreshed the page.
(Which basically I could just copy the source code from the form I made :P)
No, don't copy the source code.. because his example used hidden input fields. This would resubmit the former postdata along with post['something'] but be seamless to the end user.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

couldn't that be achieved much easier using sessions?
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Maybe you could explain more about that session example?
Post Reply