473,396 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Destroy $_POST vars after use - is it possible?

Hi,

How would I go about destroying POST vars after their use in a script?
Is this possible?

The reason is that when I use a script that, for example, add a row to
a database. If for some reason the user refreshes the page, it will add
another row. Is it the browser that resends the POST vars when a page
is refreshed?

Whats the common way to avoid this?

Cheers

Burnsy

Jul 17 '05 #1
9 22520
The common way to avoid this is check the table for the values before
entering them again. That way, it doesn't matter if the user refreshes at
all.
<bi******@yahoo.co.uk> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi,

How would I go about destroying POST vars after their use in a script?
Is this possible?

The reason is that when I use a script that, for example, add a row to
a database. If for some reason the user refreshes the page, it will add
another row. Is it the browser that resends the POST vars when a page
is refreshed?

Whats the common way to avoid this?

Cheers

Burnsy

Jul 17 '05 #2
bi******@yahoo.co.uk wrote:
How would I go about destroying POST vars after their use in a script?
Is this possible?

The reason is that when I use a script that, for example, add a row to
a database. If for some reason the user refreshes the page, it will add
another row. Is it the browser that resends the POST vars when a page
is refreshed?


After processing redirect to the destination using header/location.

Jul 17 '05 #3
Daniel is absolutely right, the most effective way to prevent reposting is
to process the form variables and then redirect the browser to the
destination page. If the user refreshes, the destination page will be
refreshed and not the script that processes the form.

To do this, you will have to make sure that you have not sent any headers
BEFORE redirecting. Any kind of HTML output such as echo or print will cause
headers to be sent automatically, so you must process the form variables
without using them. Note also that when you use the header redirect all the
local PHP variables will be reset, including any notice or warning/error
message that you have created while processing the input. Therefore, you
must use sessions to save any variables you intend to use on the destination
page. You should also call exit() after redirecting.

To be exact, do something like this:

<?
// this point must be the start of the script (or no headers sent before
this line)
session_start();
if(isset($_POST))
{ // process form variables
// ... insert into table, etc
// ... set session variables you want to keep using
$_SESSION["pagevars"] = ...
header("location: destination_url.php");
exit;
}
// ... code to run if no form was submitted:
?>

ECRIA
http://www.ecria.com


Jul 17 '05 #4
bi******@yahoo.co.uk wrote:
Hi,

How would I go about destroying POST vars after their use in a script?
Is this possible?

The reason is that when I use a script that, for example, add a row to
a database. If for some reason the user refreshes the page, it will add
another row. Is it the browser that resends the POST vars when a page
is refreshed?

Whats the common way to avoid this?

Cheers

Burnsy


You can do this by simply using unset($_POST); since its just a normal
array of varibles...
Aug 2 '05 #5
*** daemon wrote/escribió (Tue, 02 Aug 2005 08:38:02 GMT):
The reason is that when I use a script that, for example, add a row to
a database. If for some reason the user refreshes the page, it will add
another row. Is it the browser that resends the POST vars when a page
is refreshed?


You can unset $_POST variables the same way as regular variables. However,
when user loads a page using the POST method variables are created again:
remember HTTP is a stateless protocol (all connections are independent).

You should redesign your app so user cannot break things when reloading.
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Aug 2 '05 #6
On 2005-08-02 10-38-02 daemon <d4****@shaw.ca> wrote:
bi******@yahoo.co.uk wrote:
Hi,

How would I go about destroying POST vars after their use in a script?
Is this possible?

The reason is that when I use a script that, for example, add a row to
a database. If for some reason the user refreshes the page, it will add
another row. Is it the browser that resends the POST vars when a page
is refreshed?

Whats the common way to avoid this?

Cheers

Burnsy


You can do this by simply using unset($_POST); since its just a normal
array of varibles...


You could either send back a 205 Reset Content (see
<http://www.faqs.org/rfcs/rfc2616.html>) to clear the form or use a 307
Temporary Redirect (same page), to redirect the user to another page after
processing the form data.

HTH,
Simon
--
Simon Stienen <http://slashlife.org/>
"What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done."
/Sherlock Holmes in A Study in Scarlet by Sir Arthur Conan Doyle/
Aug 2 '05 #7
Ok, heres my plan...

I submit a form whos action is to a page called action.php. This script
is capable of handeling many forms on differnent pages and each is
processed using GET parameters such as action.php?action=update (will
call the update code in action, see below)

if (isset($_GET['action'])) {
if ($_GET['action'] == "update") {
//update code goes here
//now redirect them
}
}

The conditional statement if $_GET['action'] == "update" will do the
updating stuff where after it will use php/header to redirect them to
the page which will tell them that the item has been updated. This way,
if the user refreshes after the redirect nothing else will happen.

This, so far, is working a treat. Theres only one problem though. If
the user doesnt fill in every required field I would like to display
and error message on the form page. This will require redirecting the
user and displaying an error message. No probs. How do I also send back
the $_POST variables with the redirect? I dont want to have to ask the
user to fill in the fields again. Is this possible?

Feel free to make comment on my plan as it is much appreciated. Thanks

Burnsy

Aug 9 '05 #8
On 2005-08-10 00-34-40 <bi******@yahoo.co.uk> wrote:
Ok, heres my plan...

I submit a form whos action is to a page called action.php. This script
is capable of handeling many forms on differnent pages and each is
processed using GET parameters such as action.php?action=update (will
call the update code in action, see below)

if (isset($_GET['action'])) {
if ($_GET['action'] == "update") {
//update code goes here
//now redirect them
}
}

The conditional statement if $_GET['action'] == "update" will do the
updating stuff where after it will use php/header to redirect them to
the page which will tell them that the item has been updated. This way,
if the user refreshes after the redirect nothing else will happen.

This, so far, is working a treat. Theres only one problem though. If
the user doesnt fill in every required field I would like to display
and error message on the form page. This will require redirecting the
user and displaying an error message. No probs. How do I also send back
the $_POST variables with the redirect? I dont want to have to ask the
user to fill in the fields again. Is this possible?

Feel free to make comment on my plan as it is much appreciated. Thanks

Burnsy


You should validate the input by the same script which shows the form, so
you can regenerate the form on error and write all the values back into the
fields.

HTH,
Simon
--
Simon Stienen <http://slashlife.org/>
"What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done."
/Sherlock Holmes in A Study in Scarlet by Sir Arthur Conan Doyle/
Aug 9 '05 #9
> You should validate the input by the same script which shows the form, so
you can regenerate the form on error and write all the values back into the
fields.


Thanks, that should do it.

I think I shall keep the action.php page for submissions that dont
reuire any form validating, such as a form of checkboxes. Anywathing
that requires data checking I shall do on the same page as the form,
upon successful submission and data checking I shall redirect them to
the display message page.

Cheers

Burnsy

Aug 11 '05 #10

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

Similar topics

2
by: ozeh | last post by:
Hi, when I used metod post in a form, then i read posted variable and try to print, I have : In FORM text(variable_nr1) = 'test_var' In php <?php echo $_POST; ?> on screen :
10
by: arun.kumar.varma | last post by:
Hi, I'm learning PHP and prepared a simple login form. validate.php does the validation and it was behaving erratically. So, I did var_dump of $_POST variable and it's NULL. Did I miss anything...
8
by: vvenk | last post by:
Hello: I just wrote my first ASP.Net application. It worked fine on my machine and when I put into production, the ASP.Net process reaches 50% quite fast and then the system does not work...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to set up a 'control panel' consisting of a table of icons. The early stages: http://www.deepinit.com/controlcentre.php Each of these is set up like: <td> <input...
1
by: RDizzle | last post by:
okay. so all i am doing is changing a registration script that uses $_GET to a script that uses $_POST, but the validation script now returns NULL values for all posted vars. What's the deal? ...
4
by: www.webpolis.com.ar | last post by:
Hi! I have this code: <form action="get.php" method="post" enctype="text/plain"> <p align=center><strong>NUMERO DE CLIENTE</strong></p> <p align=center><input maxlength=10 size=10 type=text...
7
by: amygdala | last post by:
Hi all, I'm starting this new project in which I'ld like to implement sort of a design pattern I have seen being used in the CodeIgniter framework. Basically, the site will examine the URI and...
3
by: jishara | last post by:
How to write a program in a one page to find the sum of two numbers using php $_POST method.
7
by: gamernaveen | last post by:
I've been busy coding a signup system with PHP. There are 2 forms , form1 and form2. form1 contains: Name , Age , form 2 contains Username , Password , Repeat Pass , Email I use POST method...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.