PHP avoid browser reposting $_POST on page refresh?

£可爱£侵袭症+ 提交于 2019-12-07 02:32:32

问题


I wonder what are the techniques i can use to avoid users to post form twice when they refresh page and chose submit again?

e.g. i have form inside regiter.php and process it as well inside register.php.

1st i could process in another file e.g. register_process.php and redirect to register.php, but then i have to create about 20 new pages and relocate a lot of code, i dont want that option.

2nd i could play with headers i dont remember exact trick but had some bad experience with that - users seen old data on page after refreshing it...

3rd i could just redirect upon success to some dummy.php and from dummy.php jump back to register.php then even if they refresh page browser would not re-post, however it does not protect against them using back button and choosing re-post, i know i could expire page, but i find that annoying experience for me and probably other users to see page expired error.

4th use some unique "access key" for each form once page loaded that will post with form and once used cannot be reused, however i kind of struggle with logics of that feature. how do know key was used without storing it in MySQL DB, i think time based accesis not great either because some users can take long between page open and form submit.

I need more suggestions how to avoid users reposing form again.


回答1:


Try this:

<?php
session_start();
if( strcasecmp($_SERVER['REQUEST_METHOD'],"POST") === 0) {
  $_SESSION['postdata'] = $_POST;
  header("Location: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
  exit;
}
if( isset($_SESSION['postdata'])) {
  $_POST = $_SESSION['postdata'];
  unset($_SESSION['postdata']);
}

This will basically save the POST data and cause the browser to re-request as a GET request.




回答2:


5th. Use AJAX or jQuery and when the form is clicked on the page submit that data in the background. Output a response to the screen. Mark that form as submitted, or save to a session, and when they refresh they will not be able to submit the form again.

In my opinion it is the best way to do it anyway. I had a scoreboard with 20 or more forms it is worked really well to send the data without refreshing. You can return a response and make the page look very professional. Using jQuery you can also use some great form validation to make sure that they are submitting the fields that are required.




回答3:


When you consider the first option, I don't know about the design (programming design ^^) of your website, but you should only need 1 page. Let's says you call redirect.php with all the parameters, you should call your controller and your controller should know regardless of the parameters what to do with them.

It is a good habit to be able to do some abstraction and good design when programming, it help alot in those situation.




回答4:


I think that the best solution would be something like this:
* create a md5 or base64 of posted data
* compare this hash with a session variable (let's call it $_SESSION['repost'])
* if hashes match, skip whatever this save should do and output a warning
* if hashes do not match, or hash is not present
* assign session variable with current hash
* do whatever save should do




回答5:


Other way is store the last post in the session variable, and check if is equal, like this:

<?php
... program ...
if ($_SESSION['PREPOST'] == $_POST) {
    // DO SOMETHING
}
... program ...
$_SESSION['PREPOST'] = $_POST;
?>


来源:https://stackoverflow.com/questions/13889198/php-avoid-browser-reposting-post-on-page-refresh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!