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.

    In The News:

    Senior Web Developer  Pedestrian TV
    Full Stack Web Developer  Charlotte Agenda
    Front End Developer  IT-Online

    Five Steps to Create Your Software Product with Outsourcing

    Many executives and investors are skeptical that software products can... Read More

    Instant Relevant Web Site Content - No Its Not a Bot

    In about the Time it takes to Watch a Reality... Read More

    Build or Buy a CMS?

    Creating a content management system either from scratch,... Read More

    Developing State-enabled Applications With PHP

    Installment 1Developing State-enabled Applications With PHPWhen a user is browsing... Read More

    Make Your Web Site User-Friendly!

    Making a web site is easy. Making a good web... Read More

    Be Creative Before You Purchase Or Create A Website

    Before you jump into spending coutnless hours on designing and... Read More

    How to Promote Your Law Firm Website On the Internet for Maximum Profit

    Making maximum profit from your law firm website is important,... Read More

    Website For The Company Store

    If your company own one or more stores, you might... Read More

    Increase Your Affiliate Earnings with a Smart Webmaster Plan

    Affiliate programs are a great way to earn an income... Read More

    Web Analytics - Getting it Right

    Understanding and using web analytics.In recent years, website marketers were... Read More

    Making Your Website Shine! Attract and Keep Visitors at Your Site!

    When you consider the billions of individuals and companies on... Read More

    Effective Webdesign

    D.zigns dzignerwebs The Basics Before starting on... Read More

    How To Get Profits From Your 404 Page Not Found File

    "'Page Not Found' on this Server. Check the URL and... Read More

    Give Your Business a Cash Infusion

    Whether you have your own business already, or are looking... Read More

    From Search to Sale

    The Internet car buyer is an elusive animal. Trying to... Read More

    Web Site Optimization: How To Speed Up Your Web Site By Minimizing Your GIF and PNG Image Bit-Depth

    Are bloated images slowing down your web site and causing... Read More

    Learn to Build a Better Online Business Website Using Keywords and Content

    Are you aware that 90% of the websites on the... Read More

    Set Up Your Website in Minutes ? For Free

    There was a time not long ago when setting up... Read More

    7 Reasons Why Your Small Business Needs A Web Site

    Many small businesses have the misconception that their business can... Read More

    Build a Web Site that Works...Even for a Safari Hobby!

    My life's greatest passion is a safari hobby. And what... Read More

    Building a Web Store on Shoestring Budget in less than Four Hours

    Starting a business online is no longer as difficult as... Read More

    Improve Conversion Rates ? Load Times

    Your site is getting traffic, but conversion rates are horrible.... Read More

    How To Convert More Sales On Your Website

    One of the biggest mistakes that most online retailers make... Read More

    Web Site Promotion: How To Advertise Your Website For Profits

    Making money with your web site is the dream and... Read More

    10 Tips For A Successful Website

    Have you ever thought the secret behind the success of... Read More