title: Preventing Duplicate Record Insertion on Page Refresh PHP
body: <p>I could see there were several topics created on this matter, but I couldn't really find a solution what could be applied to my case.</p>
  
  <p>My problem is that when a user submits a form on my page, and the browsers stalls some users try to refresh the page while the request is being processed and rows are inserted twice in the database. There are two curl calls in the middle of the script what can take several seconds to complete that's what causes the delay. I beleive that by refreshing the page they open a new connection with the same session details and submit it again, but the abandoned backend script is still working on the process. And the most important (and bad) thing is, the the form posts into itself.</p>
  
  <p>We tried to apply several measures (wiping the SESSION keeping track of a generated session variable etc...) I beleive a hidden field on the form is not an option as the process script could then be attacked with a forged browser. </p>
  
  <p>My suggestion would be to detach the process part from the from the view and migrate it to separate script and call it via Ajax. While the Ajax is being called disable the submit button a prompt the user not to refresh the page while it's being processed. This way even if they refresh the page it won't get submit again and by that time the process can set the SESSION variables to prevent resubmission.</p>
  
  <p>What's your views on this solution?</p>
  
  <p>Please <strong>do not</strong> post me things like this is bad or that is bad with the process and change this or that as I am not able to make any major change in the process as a whole system builds upon it. </p>
  
  <p>Many Thanks.</p>
  
  <p>//////////////////////////////////////
  /////////////////////////////////////</p>
  
  <p>Extension:</p>
  
  <ul>
  <li>Migrated the process part of the self-posting monster script into an Ajax one.</li>
  <li>After the ajax is fired I disable the submit button and display a spinner with the text do not refresh the browser</li>
  <li>when the operation is completed in the Ajax script I set a session variable indicating that the process has been completed</li>
  <li>If they still refresh the browser, the form data is cleared and they simply won't have enough time to fill it out again, so by the time they come to submit the form again the session variable is already set and on pressing the submit button they are presented with a message and then redirected to the confirmation page.</li>
  </ul>
  
  <p>So far it seems it's working fine had a couple of test runs. </p>
  
  <p>What do you guys think about this solution? Are there any loopholes or security implications? (Error message is coming from the script via Json)</p>