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:


    pen paper and inkwell


    cat break through


    Thinking Of Taking Money Online? Whats Best For Merchants? PayPal, WorldPay or Something Else?

    It's now easier than ever to take payments on the... Read More

    PIM Team Case Study: Creating Text Effects With PHP and GD

    See how you can create graphic effects on text with... Read More

    Website Sales: 10 Reasons Why People Dont Buy From You

    You've put up a website to promote a product or... Read More

    Five Question-Five Minute Web Makeover Quiz

    If you haven't made the sales and built your clients... Read More

    Beware the Software Siren

    I've heard several prominent web marketers mention in their classes... Read More

    UK Online Shopping with an Edge, for Buyers and Sellers Alike

    Any new website competing on the internet, needs to take... Read More

    What is SQL?

    What is SQL? SQL stands for Structured Query Language and... Read More

    How Did You Get Here?

    When marketing your website, it's important to know which of... Read More

    Server Side Includes (SSI) with ASP

    Server Side Includes (SSI) with ASP People often... Read More

    Ever Wondered What Challenges Other People With Their Web Site?

    Have you ever wondered what challenges are faced by other... Read More

    7 Reasons Why Your Small Business Needs A Web Site

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

    PHP Redirect

    A PHP Redirect automatically transfers a web user from one... Read More

    Get to Know People through Websites

    Web sites are not just places to sell things; they... Read More

    Successful Web Development: 10 Key Elements

    There are many elements that are key to successful web... Read More

    Your Future Profit Is In The Stars

    You don't believe in astrology but you check your horoscope... Read More

    4 Steps to Selling More With Your Web Site

    Most web sites are more like stage sets than real... Read More

    Things to Consider Before Starting a Website

    In the time that I have been using the Internet,... Read More

    Your Website Reflects Your Business

    Some left shoes are in isle 5, while the right... Read More

    Find the Purpose of Your New / Proposed Website

    Absolutely everything stems from the purpose of your website -... Read More

    Website Imperatives and Solutions

    When you take a look at the most visited sites... Read More

    Set Up Your Website in Minutes ? For Free

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

    Nonsense! Why Do I Need a Website?

    "I don't sell anything online, I don't advertise online, in... Read More

    Adding Word Rich Content to Your Website

    You have permission to publish this article electronically, in print,... Read More

    Traffic for Webmasters

    "If you build it, they will come"; is an age... Read More

    Guide to Internet Business - Website Setup

    After planning the design and content of your website, it's... Read More

    The Birth of a Professional Web Site

    The Internet has opened a whole New World of opportunity... Read More

    Why You Need A Web Site

    "I have a small business with just a couple of... Read More

    Get a Website!, Register Your Domain!: To Website or Not to Website?

    If you're fairly new to the Internet scene, you may... Read More

    How Worldly is Your World Wide Web?

    The growth in Internet usage around the world is astounding... Read More

    Six Basic Reasons Why Visitors Stay On Your Web Site

    1. The first page appears quickly.It's a good way to... Read More

    Entangled in the World Wide Web

    I am just a learner who likes to delve in... Read More

    Five Essential Questions to Answer Before Creating Your First (or second...or third) Website

    Right after the thought, "I need a website," people usually... Read More

    How to Draw Icons or Images on a Mapserver Generated Map

    In this example I have used the map of the... Read More