Using Cookies

You can easily build on the last example to make it more useful in the real world. For instance, you wouldn’t want the Time Started value to be reset every time the user visited the first page; you probably want the value to be recorded only the first time. So it would make sense to first test for the cookie’s existence and only set the cookie if it doesn’t already exist. It would also make sense to remember the full date/time value of the user’s first visit, rather than just the time.

So, instead of

<cfset COOKIE.TimeVisitStart = timeFormat(now(), "h:mm:ss tt")>

you could use

<cfif not isDefined("COOKIE.VisitStart")>
 <cfset COOKIE.VisitStart = now()>
</cfif>

In fact, the isDefined test and the <cfset> tag can be replaced with ...

Get Adobe ColdFusion 8 Web Application Construction Kit, Volume 1: Getting Started now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.