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
    script reference

    shuttle to Midway Beardstown .. Lockport Chicago limo O’Hare
    In The News:

    The FBI has issued a warning that scammers are impersonating doctors, police and banks using spoofed numbers as "smishing" texts surge nationwide.
    Communities with unreliable internet service stand to gain after Amazon's Project Kuiper launched its first full batch of satellites into orbit April 28.
    Tech expert Kurt “CyberGuy" Knutsson says unsubscribing from spam might actually backfire. Learn when to avoid it and stop junk effectively.
    Tech expert Kurt “CyberGuy" Knutsson talks about how the first nonstop beating heart transplant, called zero ischemic time, at Taiwan hospital changes surgery.
    Kurt “CyberGuy" Knutsson: FBI warns of "time-traveling" hackers.
    Kurt “CyberGuy" Knutsson talks about a soft, vine-like robot called SPROUT that aids safe survivor rescues in collapsed buildings.
    Health insurance giant Blue Shield of California confirmed it had been sharing private health data of 4.7 million users with Google for three years without even realizing it.
    Delta and JetZero’s blended wing body aircraft marks a meaningful step toward a cleaner, quieter and more efficient future for air travel.
    Recent reports show many common passwords can be cracked in literally seconds. Kurt the CyberGuy explains how to strengthen your passwords.
    Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
    Iron, a robot that stands 5 feet, 8 inches tall and weighs 154 pounds, combines advanced artificial intelligence with human-like movement and exceptional vision.
    Hertz, the rental car giant, recently confirmed that customer information was exposed through a cyberattack on one of its software vendors.
    There are a number of features with AirPods you may or may not know about to take your listening experience to the next level. Kurt the CyberGuy explains.
    These 35 Chrome extensions have privacy and security concerns. Tech expert Kurt “CyberGuy" Knutsson says to delete them now.
    Tech expert Kurt “CyberGuy" Knutsson says 329,000 mph fusion rocket promises to be fast, disruptive and enable deep-space missions.
    Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
    Tech expert Kurt “CyberGuy" Knutsson says a new autonomous AI is a game changer that also raises privacy risks. Is your data safe?
    Tech expert Kurt “CyberGuy" Knutsson says robots and drones are revolutionizing fruit farming with faster picking and smarter handling.
    Landmark Admin revises May 2024 cyberattack scope to show twice as many people were affected. Kurt “CyberGuy" Knutsson gives tips to help stay safe from an insurance data breach.
    Tech expert Kurt “CyberGuy" Knutsson talks about how Yamaha’s hydrogen outboard motor could revolutionize boating with zero emissions.
    Tech expert Kurt “CyberGuy" Knutsson reveals how to memorialize or remove a deceased loved one’s Facebook account and protect their digital legacy from misuse or scams.
    Tech expert Kurt “CyberGuy" Knutsson says an Apple Watch saved psychiatrist Amanda Faulkner by detecting deadly leukemia early.
    Scammers and fraudsters are increasingly targeting the most vulnerable, especially nursing homes and the personal data of their residents. Kurt the CyberGuy has safety tips.
    Infected USB flash drives can spread malware among multiple organizations in ways that can easily bypass traditional security systems.
    With a fully automated warehouse system and AI-powered robots, Ocado's Hive picks, packs and delivers grocery store orders in just a few minutes.

    Website Success is Key

    The popular search engine Google indexes over 8 billion websites.... Read More

    10 Simple Steps: Its Magic If You can E-Mail You can Update Your Web Page Dynamically

    Dynamically Update Your Web Pages Via E-MailIf you can Send... Read More

    Why a CSS Website Layout Will Make You Money

    Although CSS layouts have been around for years, they haven't... Read More

    Where?s Your Web Site?

    This simple question can take on several different forms, but... Read More

    How One Word Or Even One Letter Can Boost Conversion Rates By Over 400%!

    Recently I was reviewing the keyword specific conversion rate data... Read More

    Website Globalization

    Globalize Your WebsiteThe Internet has unlocked a wide array of... Read More

    Stop Losing Precious Web Site Traffic to the Dreaded World Wide Web Black Hole

    You work hard to build traffic to your web page.... Read More

    7 Profit Producing Reasons Why Every Successful Service Professional Needs A Web Presence

    Your business website is a critical aspect of being able... Read More

    Seven Daft Things Not To Do To Your Website

    1. Splash pagesMost of the time splash pages are uncalled... Read More

    How to Get Your Website In Front of Thousands of Customers At No Cost To You!

    Dan Kennedy once said that if you can't make money... Read More

    Free Isnt Always Good - 5 Reasons Why You Need Your Own Website

    Often time I've browsed sites and seen the same thing... Read More

    Why do I Need a Website?

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

    Are You Losing Sales Because of These 4 Simple Website Mistakes?

    Losing website sales and traffic is sometimes simply a case... Read More

    How to Create Sizzling Sales online

    "Don't Sell the Steak, Sell the Sizzle."Have you heard of... Read More

    Making The Business Case For Web Standards

    Through the explosive growth of the Web, companies have realized... 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

    Website Content & Usability

    Writing for the web is totally different to writing for... Read More

    Optimal Website Design

    Optimal website design is the art of logical navigation.It will... Read More

    The Importance of Website Stats to You

    One of the best tools you have as a webmaster... Read More

    Top 7 Tips for Building an Antique Car Website

    Like wine cars get more attractive to collectors as years... Read More

    Selecting a Web Content Management Product

    So you want to take a look at a real... Read More

    Are Web Graphics Stealing Your Money?

    They might not be wearing a mask and carrying a... Read More

    Up The Sandbox!

    Go to any internet marketing forum you want these days... Read More

    From Concept to Website

    You've decided to create a website to market your products... Read More

    Whats Your Business Online Attitude?

    This article was prompted by some customer support work I... Read More

    Green Bay Hummer H2 SUV rentals ..