Most of the times we feel like maintaining the control flow of the application and prevent users not use browser control especially the browser Back button. Control flow sequence is particularly important to preserve when form submission involves transaction processing on the server which might lead to inconsistencies.
People familiar with Struts framework in Java might be aware of the Synchronizer Token Pattern available to take care of this very problem. I tried if not same a similar principle in ColdFusion to get over the problem on hand for me.
The solution described below is in no way a full blown Synchronizer Token Pattern that might be implemented in Struts but a very simple approach to solve a very mundane problem. The basic idea is to set a token in a session variable before returning a (transactional) page to the client. This page carries the token inside a hidden field. Refer code below :
<cfset variables.timer =gettickcount()>
<cfset session.timer =variables.timer>
<form name="a" action="2.cfm" method="post">
<p>Name : <input type="text" name="name"></p>
<p>Age : <input type="text" name="age"></p>
<cfoutput><input type="hidden" name="timer" value="#variables.timer#"></cfoutput>
<p><input type="submit" name="Sumbit"></p>
</form>
Upon submission, request processing first tests for the presence of a valid token in the request parameter by comparing it with the one registered in the session. If the token is valid, processing can continue normally, otherwise an alternate course of action is taken. After testing, the token resets to null to prevent subsequent submissions until a new token is saved in the session.
<cfif structKeyExists(form,"fieldnames") and form.timer eq session.timer>
<cfoutput> Your name is #form.name# & age #form.age#</cfoutput>
<cfset session.timer=0>
<cfelse>
<p>Your session is expired<p>
</cfif>
<p><a href="javascript:history.back()">Go Back (Simulate Browser Back)</a>
| <a href="1.cfm">Preferred Back</a></p>
Wanna try a demo
Please note that this is just an idea implementation I did in less than 5 minutes and has scope for improvement. All suggestions/comments are welcome.