Defines cookie variables, including expiration and security options.
<cfcookie name = "cookie_name" value = "text" expires = "period" secure = "Yes" or "No" path = "url" domain = ".domain">
cfparam,
cfregistry,
cfsavecontent,
cfschedule,
cfset
Cookies written with cfcookie
are written to the cookies.txt
file when the browser session ends. Until the browser is closed, the cookie resides in memory. If you do not have an expires
attribute in a cfcookie
, the cookie exists until the browser is closed. It is never written to the cookies.txt
file.
Warning Do not set a cookie variable on the same page that you use the |
<!--- This example shows how to set a cfcookie variable, and also how to delete that variable ---> <!--- First select a group of users who have entered comments into the sample database ---> <cfquery name = "GetAolUser" dataSource = "cfsnippets"> SELECT EMail, FromUser, Subject, Posted FROM Comments </cfquery> <html> <head> <title> cfcookie Example </title> </head> <body bgcolor = silver> <H3>cfcookie Example</H3> <!--- if the URL variable delcookie exists, set the cookie's expiration date to NOW ---> <cfif IsDefined("url.delcookie") is True> <cfcookie name = "TimeVisited" value = "#Now()#" expires = "NOW"> <cfelse> <!--- Otherwise, loop through the list of visitors, and stop when you match the string aol.com in the visitor's e-mail address ---> <cfloop query = "GetAOLUser"> <cfif FindNoCase("aol.com", Email, 1) is not 0> <cfcookie name = "LastAOLVisitor" value = "#Email#" expires = "NOW" > </cfif> </cfloop> <!--- If the timeVisited cookie is not set, set a value ---> <cfif IsDefined("Cookie.TimeVisited") is False> <cfcookie name = "TimeVisited" value = "#Now()#" expires = "10"> </cfif> </cfif> <!--- show the most recent cookie set ---> <cfif IsDefined("Cookie.LastAOLVisitor") is "True"> <P>The last AOL visitor to view this site was <cfoutput>#Cookie.LastAOLVisitor#</cfoutput>, on <cfoutput>#DateFormat(COOKIE.TimeVisited)#</cfoutput> <!--- use this link to reset the cookies ---> <P><a href = "cfcookie.cfm?delcookie = yes">Hide my tracks</A> <cfelse> <P>No AOL Visitors have viewed the site lately. </cfif> </body> </html>