Preventing Repeat Form Submission Using PHP Sessions


We've all seen those messages on some websites warning not to click a button more than once or negative consequences, like paying a bill twice, may result. Sometimes we can cause these problems by hitting the back or refresh buttons. In this article I will explain a methodology whereby a site can ensure each form is submitted only once, thereby demonstrating that such warnings are unnecessary and, depending on the nature of the problems caused, worth repairing immediately. Let's begin by taking a look at the process we are studying: Form Submission. As pedantic as it may seem, it will be worthwhile to detail each of the steps in this process:

  • Visitor requests a page from the server which has a form on it.
  • Server retrieves form and sends to user.
  • User enters data on form and submits to server.
  • Server processes form data and returns resultant page.
  • The scenario we now need to analyze is when the user re-triggers a previous form submission process. What we need to find or create is something which changes during the form submission process which does not depend on the specific form being submitted and which we can tell changed. That was a loaded sentence which fully details our solution, so let's break it down. Find or create something which
  • changes during the form submission process,
  • does not depend on the specific form being submitted, and
  • we can tell changed.
  • Since the item which changes does not depend on the form being submitted (e.g. it doesn't matter if it's a newsletter registration form, customer signup form, payment form, etc.), the item is not something which already exists and therefore must be created, so let's create a form variable called submissionId and assume it has the 3 properties mentioned above. So far, so good -- or so it appears! The third "property" is that "we can tell [it] changed", but "changed" is not a property of a variable, so we need to look at this more closely. In order to tell something changed, we must have a reference point, an answer to the question "changed from what?" This is where a session variable will come into play. If we define a session variable, say $_SESSION['nextValidSubmission'] and treat it as a reference point, we will have all of the tools necessary to protect our visitors. The idea will be to keep the session variable updated with the last submissionId sent out and change the submissionId each time it is sent out to the user. Then, if they try to resubmit the data, they will be submitting an old submissionId which doesn't match nextValidSubmission and we will know not to re-process this data. Let's look at this in terms of the processes:

  • Visitor requests a page from the server which has a form on it.
  • Server retrieves form, generates a new submissionId which is embedded into the form, updates nextValidSubmission, and sends to user.
  • User enters data on form and submits to server.
  • Server processes form data, changes nextValidSubmission, and returns resultant page.
  • Now, if the visitor somehow resends the data, they will be sending the old submissionId which will not match the new nextValidSubmission. So, you can now say goodbye to relying on javascript to remove/disable buttons, silly warning messages, and upset customers by preventing form re-submission.

    Webmaster of www.script-reference.com/">Script Reference - The *NEW* PHP Reference & Tutorial Site For Non-Programmers
    www.script-reference.com/repeat_form_submission.php">See here for more detailed information, an example using PHP, and an alternate method which doesn't require sessions.


    MORE RESOURCES:
    web development - Google News
    Updated : Sat, 06 Sep 2008 16:19:32 GMT

    Chrome could spark battle over Web tools - EETimes.com
    Chrome could spark battle over Web tools EETimes.com - 22 hours ago Vembu also expressed concerns about the rise of Silverlight, an emerging Web development environment from Microsoft. If it becomes successful, ...
    Publ.Date : Fri, 05 Sep 2008 17:57:16 GMT

    Enetsys(Saffronrage) web solutions flash game development studio. - PR-Inside.com (Pressemitteilung)
    Enetsys(Saffronrage) web solutions flash game development studio. PR-Inside.com (Pressemitteilung), Austria - 2 hours ago ... presence as a solution provider in Online & Offline games along with other domains like E-learning, Web & Graphics development , 2D/3D Animation, SEO. ...
    Publ.Date : Sat, 06 Sep 2008 14:02:33 GMT

    Local Web Server For Web Development? - Slashdot
    Local Web Server For Web Development ? Slashdot - Sep 2, 2008 gwilymgj writes "I have been running Apache/MySQL for years using XAMP and WAMP on windows for web development . Recently I switched to using Ubuntu in ...
    Publ.Date : Tue, 02 Sep 2008 14:52:41 GMT

    My New Web Browser...Thank You, Google! - ITworld.com
    News 8 Austin My New Web Browser...Thank You, Google! ITworld.com, MA - 23 hours ago One thing IE 8 does have are developer tools (idea taken from Firefox) that are truly nifty to have. So, I will be using IE8 for occasional development ... Video: Tech Test: Google Chrome Lacks Polish AssociatedPress Google Chrome shifts architects' equations as V8 powers the browser SearchSOA What Cloud Computing Needs Motley Fool Financial Times - guardian.co.uk all 209 news articles
    Publ.Date : Fri, 05 Sep 2008 16:27:04 GMT

    Google Chrome: A developer's perspective - InfoWorld
    Times Online Google Chrome: A developer's perspective InfoWorld, CA - Sep 4, 2008 Rather, it's one more vote in favor of making WebKit a primary target for new Web development projects. It only makes sense to test against the engine ... Video: Tech Test: Google Chrome Lacks Polish AssociatedPress Chrome and why Gates was right to be paranoid ZDNet How Chrome puts the skids under Nokia Register MarketWatch - Wall Street Journal all 3,448 news articles
    Publ.Date : Thu, 04 Sep 2008 10:03:16 GMT

    Internet Marketing Helps Businesses Cope with Economy - MarketWatch
    Internet Marketing Helps Businesses Cope with Economy MarketWatch - 13 hours ago To meet growing demand for Website design, PRstore has boosted its Web development staff. The team now makes up one third of the agency's creative staff. ...
    Publ.Date : Sat, 06 Sep 2008 02:23:04 GMT

    JabberWerx Accelerates Client App Development with Client Libraries - TMCnet
    JabberWerx Accelerates Client App Development with Client Libraries TMCnet - Sep 4, 2008 Jabber said they developed the JabberWerx AJAX Library in collaboration with Crowd Favorite, a development and consulting firm specializing in Web -based ...
    Publ.Date : Thu, 04 Sep 2008 17:02:53 GMT

    Paid, Inc. Extends Website Development, Branding Services to ... - Business Wire (press release)
    Business Wire (press release) Paid, Inc. Extends Website Development , Branding Services to ... Business Wire (press release), CA - Sep 3, 2008 We see multiple synergies where educational organizations can utilize our proprietary ticketing system, website development , online community management, ...
    Publ.Date : Wed, 03 Sep 2008 12:32:27 GMT

    Web development with Wicket, Part 3: Many ways to persist - Java World
    Web development with Wicket, Part 3: Many ways to persist Java World - Sep 4, 2008 In this final installment in his series on Web development with Wicket, Nathan Hamblen demonstrates how Wicket's independence from any one persistence tool ...
    Publ.Date : Thu, 04 Sep 2008 18:30:49 GMT
    UBB Webdesign.com © 2008