473,405 Members | 2,310 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,405 software developers and data experts.

unset $_POST variables

I have a page that submits data to a database, and I want to make it
so that if the page is refreshed, it doesn't submit the information
again. I am trying to use unset the variables so that if the page is
refreshed, it will not submit again. I saw a post about setting a time
delay of something like 30 seconds, but would prefer just to unset the
variable. I can't seem to get it working. Here is a snippet of what I
have:

echo 'First Name: '.$_POST['First_Name'];

if (isset($_POST['First_Name']))
{
... if it is set, submit to the database
}
else
{
echo '<h2>There is no data to submit. To enter a nomination, <a
href="index.php">Click Here</a></h2>';
}

unset($_POST['First_Name']);

echo 'First Name: '.$_POST['First_Name'];
The first echo displays what was submitted in the form, the second
shows that the variable is empty. When I refresh though,
$_POST['First_Name'] contains the data from the form again.

Can someone explain how they would go about making sure that when a
page is refreshed that data is not submitted again, or explain to me a
way to get what I have started working?
Nov 20 '07 #1
4 45298
On Tue, 20 Nov 2007 17:48:52 +0100, mtuller <mi******@gmail.comwrote:
I have a page that submits data to a database, and I want to make it
so that if the page is refreshed, it doesn't submit the information
again. I am trying to use unset the variables so that if the page is
refreshed, it will not submit again. I saw a post about setting a time
delay of something like 30 seconds, but would prefer just to unset the
variable.
This question has been asked dozens of times, in the last few weeks even.
Search the archive (hint: header redirect).

Unsetting a $_POST variable would be nonsense, as it will be repopulated
on a new POST request from the browser/user/UA.
--
Rik Wasmus
Nov 20 '07 #2
mtuller wrote:
I have a page that submits data to a database, and I want to make it
so that if the page is refreshed, it doesn't submit the information
again. I am trying to use unset the variables so that if the page is
refreshed, it will not submit again. I saw a post about setting a time
delay of something like 30 seconds, but would prefer just to unset the
variable. I can't seem to get it working. Here is a snippet of what I
have:

echo 'First Name: '.$_POST['First_Name'];

if (isset($_POST['First_Name']))
{
... if it is set, submit to the database
}
else
{
echo '<h2>There is no data to submit. To enter a nomination, <a
href="index.php">Click Here</a></h2>';
}

unset($_POST['First_Name']);

echo 'First Name: '.$_POST['First_Name'];
The first echo displays what was submitted in the form, the second
shows that the variable is empty. When I refresh though,
$_POST['First_Name'] contains the data from the form again.

Can someone explain how they would go about making sure that when a
page is refreshed that data is not submitted again, or explain to me a
way to get what I have started working?

Hi,

$_POST is populated again for each request.
If a visitor refreshes a page that was the result of a previous post, a
decent browser asks the user if (s)he wants to resubmit the page.

So unsetting the superglobal $_POST is of no use at all.
I don't know where you found that trick, but my advise is to avoid any
more tips from that same author.

A better approach to your problem is:
1) create a page with the form, eg mypage.php
2) Make the action of the form mypage_submit.php
3) in mypage_submit.php do whatever it is you need to do with the posted
info, like inserting into a database.
4) When finished, direct the browser to another page, eg thankyou.php as
follows:
header("Location: http://www.example.com/thankyou.php");
exit;

This will force the browser to load thankyou.php.
If somebody presses reload on thankyou.php (s)he will see that page
again without resubmitting the form.

NEXT PROBLEM: BACK BUTTON:
Of course, they can go back using the back-button and repost it again.
Against that, not a lot can be done reliably.
If somebody really wants to resubmit the form, well, they will.
I saw a few tricks involving all kinds of headers, but in my experience
none work reliably. (I could be wrong. http can be quite confusing)

If you REALLY need to avoid this behaviour, you should start using
'tickets'.
One simple approach that uses the idea of tickets:
1) generate a random string and put it into your form with a hidden
formelement.
eg:
<input type="hidden" name="ticket" value="KJHIUTJGHGDFHKGKJGHHFFDSG">

2) Let your receiving script store this ticket in the DB, but only if
that ticket isn't found already.
Hope this sheds some light on it.

Regards,
Erwin Moller
Nov 20 '07 #3
On Nov 20, 10:58 am, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
mtuller wrote:
I have a page that submits data to a database, and I want to make it
so that if the page is refreshed, it doesn't submit the information
again. I am trying to use unset the variables so that if the page is
refreshed, it will not submit again. I saw a post about setting a time
delay of something like 30 seconds, but would prefer just to unset the
variable. I can't seem to get it working. Here is a snippet of what I
have:
echo 'First Name: '.$_POST['First_Name'];
if (isset($_POST['First_Name']))
{
... if it is set, submit to the database
}
else
{
echo '<h2>There is no data to submit. To enter a nomination, <a
href="index.php">Click Here</a></h2>';
}
unset($_POST['First_Name']);
echo 'First Name: '.$_POST['First_Name'];
The first echo displays what was submitted in the form, the second
shows that the variable is empty. When I refresh though,
$_POST['First_Name'] contains the data from the form again.
Can someone explain how they would go about making sure that when a
page is refreshed that data is not submitted again, or explain to me a
way to get what I have started working?

Hi,

$_POST is populated again for each request.
If a visitor refreshes a page that was the result of a previous post, a
decent browser asks the user if (s)he wants to resubmit the page.

So unsetting the superglobal $_POST is of no use at all.
I don't know where you found that trick, but my advise is to avoid any
more tips from that same author.

A better approach to your problem is:
1) create a page with the form, eg mypage.php
2) Make the action of the form mypage_submit.php
3) in mypage_submit.php do whatever it is you need to do with the posted
info, like inserting into a database.
4) When finished, direct the browser to another page, eg thankyou.php as
follows:
header("Location:http://www.example.com/thankyou.php");
exit;

This will force the browser to load thankyou.php.
If somebody presses reload on thankyou.php (s)he will see that page
again without resubmitting the form.

NEXT PROBLEM: BACK BUTTON:
Of course, they can go back using the back-button and repost it again.
Against that, not a lot can be done reliably.
If somebody really wants to resubmit the form, well, they will.
I saw a few tricks involving all kinds of headers, but in my experience
none work reliably. (I could be wrong. http can be quite confusing)

If you REALLY need to avoid this behaviour, you should start using
'tickets'.
One simple approach that uses the idea of tickets:
1) generate a random string and put it into your form with a hidden
formelement.
eg:
<input type="hidden" name="ticket" value="KJHIUTJGHGDFHKGKJGHHFFDSG">

2) Let your receiving script store this ticket in the DB, but only if
that ticket isn't found already.

Hope this sheds some light on it.

Regards,
Erwin Moller
Thanks. Redirecting to the thank you page will work for me.
Nov 20 '07 #4
In article <74**********************************@41g2000hsh.g ooglegroups.com>,
mtuller <mi******@gmail.comwrote:
>I have a page that submits data to a database, and I want to make it
so that if the page is refreshed, it doesn't submit the information
I asked this question here a couple days ago, under the subject
"Clearing the form submit cache"
http://groups.google.com/group/comp....fa3a600f613faa

Among some unhelpful replies, the answer is in that thread. Basically
you need to perform header("Location: ".$page_url); where $page_url is
the page that will show the result of the form submission (i.e. the form
is submitted, the data processed into a database, and the page at
$page_url reads and displays the relevant query result from the
database).

-A
Nov 21 '07 #5

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

Similar topics

13
by: Marcus | last post by:
Hi All, I was wondering if there is a way to ensure that when submitting forms and using POST vars, the page sending the form resides on the same server as the destination page specified in the...
1
by: sarah | last post by:
Hi.. I'm having a problem trying to figure out the best way to process a POST and add items to my database, and hope someone can help me... :) Background: I have a database that I will use to...
5
by: Kit | last post by:
Hi there, I am recoding a website, and I want to add a generic footer to each page, using an included file with PHP snippets. Part of that footer would be a link to validate the page using the...
2
by: Deb M. | last post by:
I am trying to find out how I can use the ASP.net framework yet still post the variables to an external script (not ASPX) after the form has been submitted. So, the ASPX page will still post to...
3
by: Lars Netzel | last post by:
Hello! I have a button, in the click event I have this code: ---------------------------------------------------------------------------- ---------------------------- Response.Write("<script...
5
by: WhatsPHP | last post by:
Hi For some reason, at random posts, the post variables don't get thru to the server. For example, if there is are two text fields: name and email... (I have register_globals on)... When I try...
3
by: Vic Spainhower | last post by:
Hello, I have an HTML table that is being constructed from a MySQL table and displays a form that includes a check box on 1 of the fields on the form for each record. I have included in this PHP...
2
by: Aaron | last post by:
I'm trying to pull data from a website and read it into a file the I can parse. I've done the before to site without post variables but I can seem to get my statments to work with the post. Can...
4
by: SupraFast | last post by:
I have 2 forms on one page. I have hidden variables in each form, with separate names, that contain values. The user decides which form to submit on. Only 1 form will be processed. Now my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.