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 www.script-reference.com/">Script Reference - The *NEW* PHP Reference & Tutorial Site For Non-Programmers
www.script-reference.com/repeat_form_submission.php">See here for more detailed information, an example using PHP, and an alternate method which doesn't require sessions.
RELATED ARTICLESProduct Promoting: Getting The Response
When you publish a content site there are times when affiliate products are not the only products you want to recommend.Situations when you'll find you'll want to promote a non-affiliate businesses' product(s).
Flash Deadly Sins (That Can Kill Your Web Business)
Looks like every client wants a Flash site these days but the reality is..
Topics to Add When Creating an Alternate and Modern Medicine Web Directory
Alternate medicine finds its own place in the hearts and minds worldwide and has a separate following to back its existence. There are many subcategories that could be added to the modern and alternate medicine website directories.
What Business Owners MUST Know BEFORE They Commit To Having A Web Site
Q1.Why would I need a web site?
A1.
Why You Need a Website
You hear a great deal about the Internet these days -- that it's revolutionised communication ..
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.
Creating a Sticky Web Site
When used to describe a web site, the term "sticky" refers to a site that is able to get viewers to stay at the site longer and come back frequently. Sticky web sites are more likely to turn viewers into customers.
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.
Why Local Service Companies Should Have a Website
The Internet is being used by local service business to improve sales today. Sales are improved because a website gives a company more credibility and in some cases generates leads.
Your Website Should Be Selling
It may come as some surprise to a traditionally off-line business that their website should be attracting new sales, or at least supporting the sales they have made. There is a common misconception that a website is more of a cost than an asset.
The Top Ten Benefits of Having a Web Site
Do you need a web site? Are you considering getting one but are unsure? Here we take a look at the main reasons why a web site could be beneficial to your organisation.1) It will give you the competitive edgeIf you do not have a web site people cannot visit it - an obvious point you might say.
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.
Web Coach Tip: What You Should Know About DIY Web Sites
Recently, a friend asked "What's the deal with those DIY companies that advertise "How to get a website with everything your business needs for under $20 bucks a month?". "How can they do that?" she said.
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 an E-Mail you can Dynamically update Your Web Pages. Following a few simple Steps and the Power of Blogs you can Mail in your Web Page Updates anytime you want too.
7 Ways to Gain a Professional Online Reputation
A professional online reputation is essential to the
flourishing of any business big or small. Many business
owners spend countless hours on the core operations of their
business forgetting the importance their website has.
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'.
You Cant Just Build A Website And Hope It Works!
Like any area of business your website needs some effort from you if its going to be successful. You'd be foolish to just build any old website and hope it works.
WYSIWYG Versus Coding: HTML With A Purpose
After years of working with entrepreneurs who developed their first web site using an image-based editor, the new world of internet marketing has placed a stronger emphasis on web development that conforms to technical concerns such as search engine optimization and multi-browser compatibility.Wigging OutWYSIWYG (What You See Is What You Get, pronounced "whizzy wig") web site editing programs can allow anyone to develop rich, full-feature web sites without a working knowledge of HTML (Hypertext Markup Language).
Five Essential Questions to Answer Before Creating Your First (or second...or third) Website
Right after the thought, "I need a website," people usually say, "I need a web designer!" Actually, before even whispering "web design," you need to answer these five questions in as much detail as possible. Once you're clear on your strategy, you can provide better information to your web designer.
Be Creative Before You Purchase Or Create A Website
Before you jump into spending coutnless hours on designing and publishing your website, or spending hundreds on having it done by anyone else you should plan out your full design (or structure). This is very important so that when it comes time to do this long-lasting project you will be well prepared.