[thelist] Friday Freebie - One-time access in CF

Minh Lee Goon v7ac at sdsumus.sdstate.edu
Fri Jul 14 13:04:08 CDT 2000


Happy Friday people! I was going to post this as a question to y'all but
I managed to squeeze a little inspiration out of the primordial goo I
call my brain, and solved it on my own. Okay, "solve" may be relative,
but I did find a way to do what I needed to do and that's "solved"
enough for me. I just thought I'd share it with the kind folk of
Evoltopia.

What I needed to do was make sure that users were submitting a form only
once, since it's an evaluation form for a class. Here's how I killed the
giant...

On the login page:
   <!--- Create a login cookie --->
   <cfcookie name="login" value="yes" expires="never">

On the form page:
   <!--- If the cookie is not defined, refresh to error page --->
   <cfif not isdefined("cookie.login")>
      <cflocation url="error.cfm">
   </cfif>
   
   <!--- Otherwise, delete the cookie and on as usual --->
   <cfcookie name="login" value="yes" expires="now">
   
On the results page:
   <!--- If the second cookie is defined, refresh to error page --->
   <cfif isdefined("cookie.login2") and cookie.login2 is "yes">
      <cflocation url="error.cfm">
   <cfelse>
      <!--- Insert data into table --->
      <cfinsert datasource="Results" tablename="Results">
      
      <!--- Create a second cookie to prevent submitting the survey
twice --->
      <cfcookie name="login2" value="yes" expires="never">
   </cfif>

Basically, it boils down to this: I defined a cookie on the first page.
The second page will look for it and, if it's not defined, forward to an
error page. If it is defined, then the first cookie is deleted and the
user is allowed to fill in the survey. When the form is submitted, a
second cookie is created that serves as a flag to show that the user had
submitted the information before and cannot just back up to the form and
resubmit the information. 

This only applies to the one browser session. If the user deletes all
cookies and tries to log in again, the database file was also changed to
show that he/she has accessed the form once before. Works pretty well,
actually. I'm sure there's a simpler way around it, and I welcome any
and all suggestions, but I'm proud of my little accomplishment.

Hope that helped someone somewhere.

</ml>

<tip type="cfcookie and cflocation">
   The <cflocation> tag causes CF to stop processing one page and
immediately returns another page to the browser. Therefore, <cfcookie>s
that are followed by <cflocation> will never reach the browser for
storage.
</tip>




More information about the thelist mailing list