How do I stop the Back and Refresh buttons from resubmitting my form?

后端 未结 13 1248
时光说笑
时光说笑 2020-11-27 15:16

I am doing web development.

I have a page to do with credit card, which when user click \"refresh\" or \"Back\", the transaction will be performed one more time, wh

13条回答
  •  [愿得一人]
    2020-11-27 15:32

    I found the above Post/Redirect/Get explanations a little ambiguous

    Here's what I followed and hopefully it helps someone in the future

    http://wordsideasandthings.blogspot.ca/2013/04/post-redirect-get-pattern-in-php.html

    Essentially the process based on the above solution is:

    1. Submit from the FORM page to the processing page (or to itself)
    2. Handle database or payment processing etc
    3. If required, store user feedback message in a session variable, possible error messages etc
    4. Perform header redirect to results page (or to original form page). If required, display custom message from processing page. Such as "Error Credit Card payment was rejected", and reset session variables.

    Redirect with something like:

    header("HTTP/1.1 303 See Other");
    header("Location: http://$_SERVER[HTTP_HOST]/yourfilehere.php");
    die();
    

    The header redirect will initiate a GET request on "yourfilehere.php", because a redirect is simply that, a "request" to fetch data FROM the server, NOT a POST which submits data TO the server. Thus, the redirect/GET prevents any further DB/payments processing occurring after a refresh. The 301 error status will help with back button pressing.

    Helpful Reading:

    1. http://en.wikipedia.org/wiki/URL_redirection#HTTP_status_codes_3xx
    2. http://www.theserverside.com/news/1365146/Redirect-After-Post
    3. http://wordsideasandthings.blogspot.ca/2013/04/post-redirect-get-pattern-in-php.html
    4. https://en.wikipedia.org/wiki/HTTP#Request_methods
    5. http://en.wikipedia.org/wiki/Post/Redirect/Get

提交回复
热议问题