We encounter countless forms on the Internet every day, and most of them do not restrict users from submitting the same form multiple times. The lack of such restrictions can sometimes produce unexpected results, such as repeating a subscription to a mail service or repeating a ballot. Perhaps some ASP beginners are not clear how to limit the repeated submission of the same form in ASP applications, so here we introduce a simple way to prevent users from submitting the same form multiple times during the current session in an ASP application.
This work is mainly composed of four subroutines, in a simpler application, you simply put the code in the containing file directly referenced can be, for those more complex environment, we at the end of the article to give some suggestions for improvement.
First, the basic work process
Let's discuss the four subroutines in turn.
(i) initialization
Here we want to save two variables in the Session object, where:
⑴ each form corresponds to a unique identifier called a FID, in order to make the value unique to use a single counter.
⑵ each time a form is successfully committed, it must store its FID in a Dictionary object.
We use a dedicated process to initialize the above data. Although each subroutine will call it later, it is actually executed once per session:
Sub InitializeFID()
If Not IsObject(Session("FIDList")) Then
Set Session("FIDList")=Server.CreateObject("Scripting.Dictionary")
Session("FID")=0
End If
End Sub
(ii) Generate unique identifiers for the form
The following function, Generatefid (), is used to generate a unique flag for a form. The function first adds the FID value to 1, and then returns it:
Function GenerateFID()
InitializeFID
Session("FID") = Session("FID") + 1
GenerateFID = Session("FID")
End Function
(iii) Registration of submitted forms
When the form is successfully submitted, it registers its unique identity in the Dictionary object:
Sub RegisterFID()
Dim strFID
InitializeFID
strFID = Request("FID")
Session("FIDlist").Add strFID, now()
End Sub
(iv) Check if the form is submitted repeatedly
Before you formally process a user-submitted form, you should check to see if its FID is registered in the Dictionary object. The following Checkfid () function completes this work, and returns False if it has been registered, otherwise returns true:
Function CheckFID()
Dim strFID
InitializeFID
strFID = Request("FID")
CheckFID = not Session("FIDlist").Exists(strFID)
End Function