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, andwe 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.
MORE RESOURCES:
|
 |
 |
 |
RELATED ARTICLES
Top 7 Tips for Building an Antique Car Website
Like wine cars get more attractive to collectors as years pass by. The fact is there are only a finite number of cars made in the world in any model and make.
Improve Conversion Rates - Load Times
Your site is getting traffic, but conversion rates are horrible. Everything seems to be working, so what's wrong? Your site may simply be to slow.
Web Measurement: What You Don't Know Would Make A Great Book
"What's in it for me?" you ask. "Why should I measure how people use my website? How does it help and what does it all mean?" The purpose of this article is to try to give you some insight into effective web measurement and to talk about the most important page of any website, the landing or home page.
Are You Being Scammed By Your Web Design Company?
This is a growing concern amongst many business owners. Does your web design company own you? This may be possible if you've allowed them to host your web site for you and also register your domain name for your company.
Create Huge Income From Your Web Site - 10 Easy Ways
The cost of setting up a web site is dirt cheap nowadays. You can register a domain for less than $2.
Selling Your Site: Outsource or Homemade?
To the starting internet entrepreneur, there is nothing more discouraging then the erroneous realization that your dream for a self sufficient business cannot be realized without a heavy investment. This may be why so many people shy away from the internet; seeing all the flashy sites, many of which are $50 templates, people are afraid to enter the world of internet sales and marketing.
9 Keys to an Effective Website
Many small companies choose not to invest in a professionally designed website, although a well-designed Web site may raise the company's level of professionalism and exposure - AND drive business and sales!Make sure your website is working for you with the following characteristics:Usability enables your users to get the most out of your site. This means that the
site's navigation is consistent, clear and easy to follow; forms and links are in
working order; and the information on the site is current and answers incoming
clients' potential questions.
You Need a Website!
No matter the size of your business, having a web site is a low cost way to market your services and products twenty-four hours a day. Web sites are a constant advertisement for your business and will help you reach your targeted customers effectively.
PIM Team Case Study: Creating Text Effects With PHP and GD
See how you can create graphic effects on text with PHP and GD - drop shadows, arcs, fonts and colors.ProblemA-tec Signs and Sraphics Inc.
Launch Your Own Website Today - Its Easier Than You Think!
Thousands of people are now making a living online and the opportunities are abundant as the Internet continues its growth. An added benefit of running an Internet business is that you can usually start one part-time while you maintain your job and steady income.
7 Golden Rules For Building Mini Sites
First, an eye opener. Making your fortune through
affiliate programs on the internet is not easy.
Web Site Promotion: How To Advertise Your Website For Profits
Making money with your web site is the dream and goal of millions of webmasters all over the internet including myself. In order to reach this goal you need a variety of web site promotions running all at the same time.
User Interface Design: Key to Achieve Best Web Development
From early days of www, the Internet sites that are added day by day are increasing tremendously. When you will consider this pool of web, may be they are about computers, industry, entertainment, search engines, fashion, organizational or even individual, all of these sites are concerned about one major part of web development apart from the logic or programming efforts behind it is 'User Interface Design'.
Search Engines and Customers Want Focused Web Site Content
How do you decide on the content, products and or services you
will promote on your Web site.Or should I go back one step further and ask why did
you decide to create a website in the first place.
Revealing the Firefox Secret Used by Top Web Developers and Power-Browsers
Profiles are one of the best kept secrets around regarding Mozilla Firefox (my browser of choice).In the role of web site designer, I find it useful to have Firefox set up with various Extensions for helping test compliance, keep track of Google Adsense earnings, checking PageRank and Alexa rating, etc.
Five Reasons You Have to Stop Your Web Site
That's right. Your method, behavior and strategy you are now using to manage your website may be justifiable reasons to stop it as soon as possible.
The Most Important Aspect of Writing Web Copy
There is an ongoing debate about web copy. Some say that it should be similar to direct mail copy.
Web Designer? You Dont Need No Stinkin Web Designer!
RIVERSIDE, CA August 4, 2004 -- "Historically, small business owners have had a limited number of options to choose from when it comes to the development of an on-line presence," says Pat Spinelli, owner of The Web Site Tutor, a Riverside, California 1. web site design and training firm.
How to Promote Your Law Firm Website On the Internet for Maximum Profit
Making maximum profit from your law firm website is important, but as a lawyer you might not have any clue about search engine optimization and Internet marketing for your legal site. However, it is not so difficult to poise your website to be successful online even if you are not experienced with Internet marketing and driving website traffic.
UK Online Shopping with an Edge, for Buyers and Sellers Alike
Any new website competing on the internet, needs to take its business seriously, as there is so much competition around. So, there needs to be an edge on quality, content, and general user appeal.
|