Here's a tool you can use to easily set a cookie at login and then protect the rest of your site from individuals who don't have this cookie. Along the way, you'll determine the previous page; delete a cookie; read a cookie; change pages; determine where you are; set a cookie; reload the current page; and use authenticate.cfm with the CONTAINS operator to protect the rest of your application.
You'll build three pages plus a throwaway "site" represented by a directory and a destination page that takes the place of the rest of your site.
Interestingly, because the CFLOCATION tag was used to bring you back here, it's as if you never left, as if the page on which it was used never finished loading. That's why you test for the current page (knock.cfm), not the page the user was kicked out of, as the referring page.
<cfif findnocase("knock.cfm",cgi.http_referer)>
<p>Your login failed.
</cfif>
<cfcookie name="key" value="" expires="now"> <form action="Gate.cfm" name="knock" method="post"> Enter employee ID:  <input type="text" name="employeeID" value="" size="10" maxlength="8"> <input type="submit" name="submit" value="OK?"> </form>
Notice that we're reading a previously set cookie, not writing one; so, it's OK to use CFLOCATION here. The "foyer" represents your site's main menu after login.
The login and authentication pages will have less automatic protection than the other pages in your site; so, you need to be especially careful to avoid errors that might strand a user here. Therefore, check for the presence of your form variables before trying to use them and bounce a user that doesn't present them.
<cfif isDefined("cookie.key")>
<cfif cookie.key eq "asdf">
<cflocation URL="foyer.cfm">
</cfif>
</cfif>
<cfif not isDefined("form.employeeID")>
<cflocation URL="knock.cfm">
</cfif>
Using the meta tag to refresh the location is ALMOST compatible with setting cookies. If you refresh to a page and let authenticate.cfm test for the cookie on that page, the test fails. Therefore, you need to refresh to a page that authenticate.cfm is not protecting with the cookie test. You could create an intermediate page whose only function is to be ignored by application.cfm and move you along to the foyer, or you can do what we've done here and refresh to the current page. The code below reloads the page. The code above sees the newly written cookie and moves to the foyer when the page reloads.
<cfset self="cgi.cf_template_path"> <cfif form.employeeID eq "frank"> <cfcookie name="key" value="asdf"> <cfoutput><meta http-equiv="Refresh" content="0 URL=#self#"></cfoutput> <cfelse> <cflocation URL="knock.cfm"> </cfif>
The only exceptions are the pages you can't secure this way (like the login and gate) or don't want to secure (like introductory help or new account requests). Exclude those pages from your test by listing them and then seeing if the filename the user's trying to reach is on the list.
Because "and" stops as soon as it encounters a failure, we can combine the test to see if the cookie exists with the test of the cookie's contents as shown. Save this code as Application.cfm to boot failures out of the application.
<cfif "KNOCK.CFM;GATE.CFM" does not contain uCase(getFileFromPath(cgi.cf_template_path))>
<cfif not (isDefined("cookie.key") and (cookie.key eq "asdf"))>
<cflocation url="knock.cfm">
</cfif>
</cfif>
Try to go to foyer.cfm. If you are not logged in, Application.cfm will dump you to knock.cfm. If you enter "frank" as the employee ID, gate.cfm will write the cookie to your browser that you need to go to protected pages. This may not look like much, but you've just used three tiny files to admit wanted users and exclude unwanted ones. It's a reasonable foundation for the rest of your security measures. =Marty=
[The tag used for refresh needs to be wrapped in cfoutput tags as shown so the variable "self" will be properly expanded.]