Web Development Information

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 Script Reference - The *NEW* PHP Reference & Tutorial Site For Non-Programmers
    See here for more detailed information, an example using PHP, and an alternate method which doesn't require sessions.


    MORE RESOURCES:

    Content Management and Web Development Manager
    Seattle Times, United States - Jun 23, 2008
    This is a direct hire opportunity in Bothell, WA with our client. Qualified candidates can submit resumes directly to lspraggins@comsys.com for ...


    zembly Provides Social Context for Web Development
    O'Reilly Radar, CA - Jun 29, 2008
    ... I'll be following zembly as they build out their community-oriented features and work to deliver on their promise to wiki-fy web development, ...


    TechJournal South

    What’s hot in Web Development?
    TechJournal South, NC - Jun 27, 2008
    Corporate blogging is another hot area in Web development, says Hoffman. “It’s something people are asking for, although they’re not always sure what ...


    Web Development Lead - Realtime Worlds
    Next Generation, CA - Jun 27, 2008
    By Jobs Applicants must have experience of working on a high-load, high-availability website. Applicants should have the technical knowledge to design ...


    Revamp of IT Chimes a Website Design, Web Development and SEO ...
    The Open Press (press release) - Jun 20, 2008
    New Delhi, India (OPENPRESS) June 21, 2008 -- IT Chimes, an award winning Web Development Company based in Delhi, India with Sales offices in New York and ...


    Over 12 Top, Free Tools For Web Development Projects
    OStatic, CA - Jun 20, 2008
    Most web development environments cater especially to developers who favor certain languages and environments. Kompozer is a huge favorite with developers ...


    Tuncer InfoTech Pvt. Ltd Launches 'Direct To India' A Offshore Web ...
    Newswire Today (press release), UK - Jun 18, 2008
    Now Tuncer InfoTech Pvt. Ltd. Launch its offshore Web Development, Web Design and Internet Marketing Company at India named Direct to India in June 2008. ...


    Job vacancy: Web Development Officer / Web Developer
    PublicTechnology.net, UK - Jun 18, 2008
    (Ref: 39JGP/LG) SCIE is looking for a Web Developer / Web Development Officer to help us deliver our new strategy. You will join a small team, ...


    Broadshout Ltd launches new job board serving the web development ...
    Online Recruitment, UK - Jun 17, 2008
    The jobboard offers candidates instant access to the latest web development job vacancies across the UK. Candidates are able to apply directly to adverts ...


    Dropouts can undertake self-study to succeed
    Shreveport Times, LA - 17 hours ago
    Dropping out of college at 17 because "I was really not emotionally ready," he worked in Web development while always "exploring new technologies and ...

    Web-Development - Google News

    home | site map
    © 2006 Webhosting-Service.com - All Rights reserved. - GetWeb Media Webdesign Muenchen