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


    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

    Websites for Writers ? Why You Need One and How to Get Started Today

    It may seem like the publishing industry's equivalent of Beanie... Read More

    Effectively Using Robots Meta Tags

    The "robots" meta tag, when used properly, will tell the... Read More

    The Web: Your Window of Opportunity

    Thinking about starting up a business? The web is the... Read More

    Web Development Processes and Technical Environments

    The process of Web application development is critical to the... Read More

    Let Marketing Manage Your Website Content

    The definition of the phrase "Content Management" depends on what... Read More

    Developing A Website: 10 Tips For Getting Started

    1. Be clear on your purpose.Building a website can be... Read More

    Developing A Login System With PHP And MySQL

    Most interactive websites nowadays would require a user to log... Read More

    Web Site Development Process - The Life-cycle Steps

    A system development process can follow a number of standard... Read More

    Web Design for the Professional Magician Part I - Selecting the Perfect Domain Name

    Selecting the Perfect Domain NameThe first issue I have with... Read More

    Beyond Web Usability: Web Credibility

    If you've been developing websites on Mars for the past... Read More

    Web Coach Tip: What You Should Know About DIY Web Sites

    Recently, a friend asked "What's the deal with those DIY... Read More

    Developing State-enabled Applications With PHP

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

    Topics to Add When Creating an Alternate and Modern Medicine Web Directory

    Alternate medicine finds its own place in the hearts and... Read More

    Have You Got Your Ear To The Ground Or Your Head In The Sand?

    What happened on your website yesterday? What about last week... Read More

    Does Your Business Really Need A Website?

    Website, website, website, everyone says you need one. But do... Read More

    Website Strategy!

    A website lets you put your products in front of... Read More

    What Does An Ex-Basketball Coach Know About Pulling Web Metrics?

    In my previous life.....Actually, my previous JOB life, I was... Read More

    Autoresponders Make You Look Like A Pro

    People like to get immediate responses to requests. Autoresponders are... Read More

    Why Your Brick-and-Mortar Biz Needs A Website

    The internet has taken over our lives with a vengeance,... Read More

    How To Keep Your Website Sticky

    Developing a website with content that entices visitors to return... Read More

    Streamline your Business Website with a Content Management System

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

    CPanel Fantastico - Features and Benefits

    Fantastico is a 3rd-party cPanel add-on and can be purchased... Read More

    How Your Own Website Helps Your Small Business Grow

    What do you mean, you don't have a website for... Read More

    Three Way Linking - Webmaster Strategy

    Three way linking and concerns.For many years SEO experts were... Read More

    Guide to Internet Business - Design and Content

    After deciding what kind of internet business you want to... Read More

    Website Development: Become Your Own Expert

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

    Warning: The Truth About Having a Web Site!

    If you don't have a web site yet, are thinking... Read More

    Web Site Promotion: How To Advertise Your Website For Profits

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

    Making Your Websites More Compelling

    The Internet is a remarkable publishing medium. With just a... Read More

    Three Things Every Website Should Do

    When I started my company in 2002, I knew I... Read More

    How to Get a Knock Out Website!

    The first step in setting up an online business...Starting an... Read More

    Link Trades That Waste Your Time

    Never has competition been so difficult in the Internet world.... Read More