Unset Post Variables After Form Submission

Unset post variables after form submission

The post/redirect/get is a good option as some posters have already mentioned.

One another way I can think of is to set a session in the dostuff.php page to indicate that the posting has already been done. Check this session var each time to see if the page is being loaded again because of a page refresh.

<?php
session_start();
if(isset($_SESSION['indicator']))
{
/*
dont do anything because session indicator says
that the processing was already done..

you might want to redirect to a new url here..
*/
}
else
{

/*
first set session indicator so that subsequent
process requests will be ignored
*/
$_SESSION['indicator'] = "processed";

//process the request here..
}
?>

In the page you redirect to, unset the session var so that the form can be resubmitted again afresh, making it a new post operation. This will allow new form posts but will prevent post operations due to page refresh

how to unset post array?

If you submit a form and then refresh the resulting page, the browser will re-post the form (usually prompts first). That is why the POST data is always present.

An option would be to store a session variable and have it sent in the form, then check if it matches in the form processing code - to determine if it is a re-post or not.

Within the form:

<input type="hidden" name="time" value="<?php echo $time; ?>" />

In the PHP:

session_start();

if(isset($_POST['submit']))
{
if(isset($_SESSION['time']) && $_SESSION['time'] == $_POST['time'])
{
echo "User name : <b> $name </b>";
}
}

$time = $_SESSION['time'] = time();

Another option is to redirect after processing the post data:

if(isset($_POST['submit'])) 
{
...
...

header('Location: ' . basename($_SERVER['PHP_SELF']));
exit();
}

How to delete $_POST variable upon pressing 'Refresh' button on browser with PHP?

The request header contains some POST data. No matter what you do, when you reload the page, the rquest would be sent again.

The simple solution is to redirect to a new (if not the same) page. This pattern is very common in web applications, and is called Post/Redirect/Get. It's typical for all forms to do a POST, then if successful, you should do a redirect.

Try as much as possible to always separate (in different files) your view script (html mostly) from your controller script (business logic and stuff). In this way, you would always post data to a seperate controller script and then redirect back to a view script which when rendered, will contain no POST data in the request header.

Why I can't unset my POST variable after showing it?

Unset is working. But in your code

<?php
if (isset($_POST['nvalue']) AND !empty($_POST['nvalue'])) { // in here you are checking is the variable is set or not

echo $_POST['nvalue'] . '<br>'; // Now here this will echo the value

unset($_POST['nvalue']); // In here you will unset the value **Please note unset is working in here**
}
else{
echo "22222";
}
?>

But the problem is that your function almost executed and when someone leave text area as empty and submit the form then

(isset($_POST['nvalue']) AND !empty($_POST['nvalue']))
this will be false and it will execute the else pharse.

So that your unset() is working

Just think if you use method="GET" then if some one enter 55 as the value your url will be like this

.../?nvalue=55

So you are refreshing this url again and again. But this line

(isset($_POST['nvalue']) AND !empty($_POST['nvalue'])) return true

You can do something like this:

<?php

if (isset($_POST['nvalue']) AND !empty($_POST['nvalue'])) {
echo $_POST['nvalue'] . '<br>';
unset($_POST['nvalue']);

}
if (!isset($_POST['nvalue']) AND empty($_POST['nvalue'])) {
echo "22222";
}

?>

If user input is 55, then the output will be

55
22222 //because your unset is working

But I can't understand what are you trying to do something like this.

Using unset for $_POST -

It is as simple as it gets. Try this:

$_POST = array();

unset $_POST doesn't works

If the user is refreshing the page they will be sending the turnOver POST value each time. Unsetting post will only affect the rest of that page.

Best thing to do is set a session value and set that the first time that turnOver is sent then check against that.

Clearing _POST array fully

Yes, that is fine. $_POST is just another variable, except it has (super)global scope.

$_POST = array();

...will be quite enough. The loop is useless. It's probably best to keep it as an array rather than unset it, in case other files are attempting to read it and assuming it is an array.



Related Topics



Leave a reply



Submit