473,545 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 22547
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.goo glegroups.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("locatio n: 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?acti on=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?acti on=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
3956
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
14674
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 here regarding the configuration or code? Code for validate.php is given below. <?php var_dump($_POST);
8
1941
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 anymore until I kill that process. Obviously, this is not acceptable. Looking back, I do not destroy any objects in my form. Would that be the reasn...
5
7899
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 type="image" id="addnews" src="/Image/add24.png"
1
6987
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? NOTE: when i use $_GET the script just works. Thanks in advance for helping a noob.
4
2298
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 name="idcliente"><br> <center><strong><font face=verdana size=2>Clave de acceso</strong></ font>
7
3356
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 based on the segments of the URI it will fire up some controller class, for instance, say I have an inbox in which end-users can view messages they...
3
7914
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
2633
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 to submit the form. Here , name and age is sent from form1 to form2 and form2 gets it through $_POST vars. The next step is submitform.php , once...
0
7490
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7682
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7935
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7780
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6009
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5351
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.