在php中刷新页面后重新提交所有的vales

I have a php form. Here i included the action page on the top and the action is php self. i have submitted the page and got the submit message. But when i refresh that page( using f5) again the pge re submitted. how i avoid this ?

It's none standard way to submit a form to itself. You have to have an extra file that will process your data and then return to your main page.

extra file:

if(isset($_POST['submit'])){
    $done = 1;
} else $done = 0;
header('Location: '.$_SERVER['HTTP_REFERER']."?done=$done");

also you can use :

header('Refresh: 5; URL="mainpage.php"'); 

if you have to post data to itself.

There are dozen possibilities how to do that - here just the first few which came into my mind:

  • hashing the input including timestamp and save that in database and check in a function if hash already exists
  • forward to another site after the input header('Location: another-site.php')...
  • save (temporary) IP, browser and timestamp and check if the re-submit is within the allowed time
  • a CAPTCHA ...
  • a combination of the things mentioned above

It depends on what fits you best ;-).

The easiest fix is to redirect to a "recipt" page either with header('location:' or if you're uploading files, with echo '[script]document.location = '...'

IE buggy when redir on multipart-form-data..

regards,

//t

We use

header("Location: ORIGINALURL"); 
die();

After submitting a form.

However, some webbrowsers will ignore a redirect to the same URL and then the refresh problem remains.

So we use:

header("Location: ORIGINALURL?timestamp=".time());
die();

Now the URLs will be unique and the page will redirect.

I didn't know IE was buggy on multipart-form-data (thanks @user247245), I will have to look into that.

If you're just trying to prevent accidental reduplication, you can use the Post/Redirect/Get pattern.