# CFCOOKIE

Defines cookie variables, including expiration and security options.

### Syntax

    <CFCOOKIE NAME="cookie_name"
        VALUE="text"
        EXPIRES="period"
        SECURE="Yes/No"
        PATH="urls"
        DOMAIN=".domain">

#### 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's browser.

#### SECURE

Optional. Indicates the variable has to transmit securely. If the browser does not support Secure Socket Layer (SSL) security, the cookie is not sent.

#### PATH

Optional. Specifies the subset of URLs within the specified domain to which this cookie applies:

    PATH="/services/login"

Separate multiple entries with a semicolon ( ; ).

#### DOMAIN

Specifies the domain for which the cookie is valid and to which the cookie content can be sent. An explicitly specified domain must always start with a dot. This can be a subdomain, in which case the valid domains will be 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`. In the case of special top level domains, only two periods are needed, as in `.allaire.com`.

When specifying a PATH value, you must include a valid DOMAIN.

Separate multiple entries with a semicolon ( ; ).

### Usage

Cookies written with CFCOOKIE do not get written to the `cookies.txt` file until 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 set exists only as long as the client browser is open. When the browser is closed, the cookie expires. It is never written to the `cookies.txt` file.

### 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 email 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>