For example, I ask this question and click “Post your question” and stay in the current page. Now I may click the “refresh” button of my browser to see new answers. But I find that on my website, if I click the “refresh” button, a duplicated question will be published. How to avoid this problem?
I am using PHP.
For example, I ask this question and click Post your question and stay in
Share
It is common practice after a
POSTrequest, to redirect to the same page to avoid this problem.Lets say you are on
/ask_question.phpYour opening
<form>tag might look like this:Now, in your
/ask_question.phpdo something like this:Update It is important to only redirect after a valid
$_POSTrequest has been handled. I test for a form field namednew-questionbut you should use any form field name that has to be present for the$_POSTto succeedThis processes their posted data, then on success, redirects back to the same page. The only difference now, is that when they click refresh, no information will be posted.
Note Just make sure nothing is
echo‘ed out prior to theheadercall.