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


    Why do I Need a Website?

    Cost Effective Advertising. Online selling. Increased geopgraphic range. Wider... Read More

    10 Tips For Running A Profitable Web Site

    1. Address your targeted audience on your business site. Example:... Read More

    Web Accessibility: The Basics

    What is web accessibility & why is it important?Web accessibility... Read More

    17 Tips to Plan a Website

    Everyone wants one.Everyone wishes they knew how to make one.SOME... Read More

    The Number One Reason Most Websites Fail

    Failure, just like success, is measured differently by each and... Read More

    How to Increase Sales with Automation Improvements

    How popular is online shopping? And is there room for... Read More

    Freelance Programming is Easy to Manage

    There are several reverse bid freelance sites out there. Beyond... Read More

    How Improving Your Sites Usability Can Increase Your Buyers by 40% and Boost Each Order By 10%!

    With all of the factors involved in designing and building... Read More

    4 Marketing Tips for Resourceful Webmasters!

    The internet is a sea of knowledge. Getting your information... Read More

    A Network Of Web Sites Is Not Enough!

    The big thing these days is your ability to capture... Read More

    Website Development: Become Your Own Expert

    Starting and maintaining a business is challenging enough without the... Read More

    Five Steps to Create Your Software Product with Outsourcing

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

    A Beginners Guide to Server Side Includes

    An Introduction to Server Side IncludesBeginner? That's OK!While much has... Read More

    Improve Conversion Rates ? Effective Content

    Your site is fast and getting traffic, but conversion rates... Read More

    Why You Really Need Your Own Web Page In-order To Make Online Business!

    How many Web Sites did you stumble over telling you... Read More

    12 Tips To Great Websites

    Why do some sites succeed while the vast majority of... Read More

    Running An Effective Website

    Running an effective website is a continuous process. I have... Read More

    Build or Buy a CMS?

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

    Why Every Small Business Owner Needs Two Websites

    Every small business owner knows that they need a website,... Read More

    WYSIWYG Versus Coding: HTML With A Purpose

    After years of working with entrepreneurs who developed their first... Read More

    Earn On Offshore Web Design and Development

    If you have some experience in web site design and... Read More

    Why Is No-one Buying From My Website?

    Are you, like many other website owners, frustrated at the... Read More

    Maintaining Your Business Website

    QUESTION: Should I build and maintain my business Web... Read More

    If You Build It, They Will Come?

    Building a new website can be a lengthy task. From... Read More

    1 Simple Solution to All Internet Marketers about Their Website Design Needs

    It is an undeniable fact that not everybody is keen... Read More

    Content Management

    More and more businesses are recognizing the importance of content... Read More

    The Secret Benefit Of Accessibility: Part 1 - Increased Usability

    Web accessibility has so many benefits that I really do... Read More

    Successful Commercial Sites

    Do you make your website just to entertain yourself or... Read More

    4 Rock Solid Reasons For Building Your Own Website

    What is the point of having a website, you may... Read More

    Web Analytics - Getting it Right

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

    Web Content Strategy 101

    Your content is what gets you in search engines, speaks... Read More

    Streamline your Business Website with a Content Management System

    I talk with so many people who have small businesses... Read More

    Setting Up A CGI Script On Your Web Site

    Before you set up any scripts you will need some... Read More