cfcookie

Description

Defines cookie variables, including expiration and security options.

Category

Variable manipulation tags

Syntax


<cfcookie name = "cookie_name"

  value = "text"

  expires = "period"

  secure = "Yes" or "No"

  path = "url"

  domain = ".domain"> 

See also

cfparam, cfregistry, cfsavecontent, cfschedule, cfset

Attributes

Attribute
Description
name
Required. The name of the cookie variable.
value
Optional. The value assigned to the cookie variable.
expires
Optional. Schedules the expiration of a cookie variable. Can be specified as a date (as in, 10/09/97), number of days (as in, 10, 100), "Now", or "Never". Using Now effectively deletes the cookie from the client browser.
secure
Optional. Yes or No. Specifies that the variable must transmit securely. If the browser does not support Secure Socket Layer (SSL) security, the cookie is not sent.
path
Optional. Specifies the URL within a domain to which this cookie applies. To specify more than one URL, use multiple cfcookie tags.
path = "/services/login"

If you specify a path, you must also specify a value for the domain attribute.
domain
Optional. Specifies the domain for which the cookie is valid and to which the cookie content can be sent. An explicitly specified domain must start with a dot. If the value is a subdomain, the valid domains are any domain names ending in this string.
For domain names ending in country codes (such as .jp, .us), the subdomain specification must contain at least three periods; for example, .mongo.stateu.us. For special top level domains, only two periods are needed, as in .macromedia.com.
When specifying a path value, you must include a valid domain.
Separate multiple entries with a semicolon.

Usage

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 cflocation tag. If you do, the cookie is never saved on the browser; therefore, it is of no value.


Example

<!--- 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>    



Banner.Novgorod.Ru