/    Sign up×
Community /Pin to ProfileBookmark

How to redirect without sending POST/GET parameters

Hi. I have a form which submits field values to a database. The process of writing to the database is in the same file as the form itself. If the form has been submitted, the data is written, if not, the form is shown.

This has been working fine, but if the user submits the form, then presses the refresh button on their browser, the data is written twice.

What i would like to do, is use

[code=php]header(‘location: http://’ . $_SERVER[‘SCRIPT_NAME’]);[/code]

to redirect to the same page on successfull data submission, so if the user presses refresh, it doesn’t matter. However, this obviously fails, as the $_POST data is carried forward with the redirect.

Is there a way to use this redirect, but to drop all POST data in the process? This is the case if i redirect to a different page, but POST data is still present when redirecting to the same page.

Any help would be appreciated.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@ShortsJul 16.2009 — If someone clicks the refresh button than you can't stop it via redirect, what you can do is set a SESSION_ID, and if the data gets stored with then change the SESSION_ID so on a refresh it won't re enter the information (and can in that case also have your code unset $_POST$_GET$_REQUEST).
Copy linkTweet thisAlerts:
@TecBratJul 16.2009 — try this:
[code=php]
<?php
//do whatever you need with the post data,
//but don't echo or print anything.
unset $_POST;
header('location: http://' . $_SERVER['SCRIPT_NAME']);
?>
[/code]


I haven't tried it, but it makes sense that it might work.?
Copy linkTweet thisAlerts:
@Phill_PaffordJul 17.2009 — Here is a redirect function to use:

PHP Code:
[code=php]function redirect($url) {
if (!headers_sent()) {
//If headers not sent yet... then do php redirect
header('Location: '.$url); exit;
} else {
//If headers are sent... do javascript redirect... if javascript disabled, do html redirect.
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
}
}[/code]


How to Call:
[code=php]$url = "/redirect.php"; // This is the variable with the URL
redirect($url); // This is using the variable in the function [/code]
Copy linkTweet thisAlerts:
@DasherJul 19.2009 — Another possibility would be to do a query to the data base and compare a field that would be expected to be unique with the same field from the POST data. If the same don't write to the database with the current data.

Or add a time stamp and if data is being attempted to be entered within a certain time frame from the same user post an error message instead of the data to the database.
Copy linkTweet thisAlerts:
@TecBratJul 20.2009 — I tried this. It worked. It wrote my file with the contents of the test var. I deleted the file and refreshed the page. The txt file was not re-created.
[code=php]<?php
/*redir_test.php*/
if($_POST['submit']){
$test=fopen('redir_test.txt','w');
fwrite($test,$_POST['test']);
fclose($test);

unset ($_POST);
header('location: http://www.mydomain.com/redir_test.php');
}?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Test <input type="text" name="test"><br>
<input type="submit" name="submit" value="Submit">
</form>
[/code]
Copy linkTweet thisAlerts:
@jorwanOct 29.2018 — @Phill_Pafford#1021778

Perfect!

This works for me, Thnks

I don't know why PHP can't redirect without sending the same previous get params

But this makes the trick, to clean previous get params in the redirect

Thanks
Copy linkTweet thisAlerts:
@rootOct 30.2018 — {"locked":true}
Copy linkTweet thisAlerts:
@rootOct 30.2018 — PLEASE DO NOT RESURRECT OLD POSTS.

Thank you.

×

Success!

Help @Typhoon101 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...