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
Website Sales: 10 Reasons Why People Dont Buy From You
You've put up a website to promote a product or
service.You've spent piles of cash to generate traffic.
Building eCommerce Websites that Work - Part 2
Succeeding with an eCommerce website is a dream for many these days. It may seem nearly impossible at times, but it can be done.
How to Build a Database Driven Web Site
If you ever want to create a state directory, article
directory, dating site or link directory, you need to know about
creating database driven web sites. This may seem like a
daunting task at first, if you have never done it, however
I was in the same boat as you.
Eight Deadly Web Site Mistakes and How to Avoid Them
Creating and maintaining an effective presence on the Web has become increasingly complex and challenging as the power of the Internet as a marketing tool becomes more and more necessary to entrepreneurs and emerging businesses.The good news is that technologies that were only available to the big guys just a couple of years ago - audio, video, ecommerce, e-mail database management applications - are now affordable for almost everybody.
How To Get Profits From Your 404 Page Not Found File
"'Page Not Found' on this Server. Check the URL and try again.
Why Do I Need A Web Site?
Even though the Internet has been around for a long time and many people are "educated" about the Internet, most have little knowledge about what a web site is and what can it do for their business.A web site is anything and everything you want it to be.
Web Content Strategy 101
Your content is what gets you in search engines, speaks to visitors, and ultimately decides the success or failure of your site. Meanwhile, your content has to be updated at least once a month if you want to get return visitors and search engine traffic.
17 Tips to Plan a Website
Everyone wants one.Everyone wishes they knew how to make one.
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.
Make Your Own Website - General Website Design Tips
Wow, we`ve already taken a domain and chosen a web host. Great job ! Now it`s time for something special: making your own website.
Your Website Reflects Your Business
Some left shoes are in isle 5, while the right shoes are in isle 3. Shoe hills are in random places.
Website Strategy!
A website lets you put your products in front of a world-wide audience. It can help you generate new revenue, cut costs and build better relationships with both customers and suppliers.
Cost Effective and Powerful Dynamic Websites w. Fantastico Web Hosting
If you are developing your website on a host that charges less that $5 a month, make plenty of backups as you are likely to loose your website files when the webhost goes out of business without notice. Indeed, be extremely wary of any such host.
How Web Templates Are Helping Online Businesses to MultiplyTheir Income
Web templates by nature are created to aid and ease the
strenuous process of a typical web development phase. Idea
collection, analysis, research, design are just a small part
of a bigger picture.
The Problem with Paypal On Your Web Page
When I set up my website I felt paypal was a very simple and easy solution when people wished to purchase my products.For 6 months I was getting 150-200 unique visitors a day, and yet only a few sales every week.
Server Stats - Analyzing Traffic To Your Site
Analyzing traffic to a site is a key factor in tweaking search engine optimization and marketing campaigns. Determining how people get to your site is critical.
Growing Your Meetings In CyberSpace
As increasing numbers of people search for information on the Internet, it becomes more imperative to have a compelling Website to promote and support your meetings.Here is my list of "The Seven Most Important Things You can do Online":1.
Stop Losing Precious Web Site Traffic to the Dreaded World Wide Web Black Hole
You work hard to build traffic to your web page. If you are not doing 1 simple step you are loosing a portion of all your web site traffic to the dreaded World Wide Web Black Hole.
Art, Artists and the Web: Part 4--What to Do After a Website is Designed
What to do if you are an artist after you finish your website.Unfortunately, just because you've created a website, does
not mean that anyone will visit it.
From Search to Sale
The Internet car buyer is an elusive animal. Trying to hunt them down for the kill is not to be taken lightly.
|