Working with Cookie Variables

Cookies are general variables used by application servers such as ColdFusion Server to store data in and retrieve data from individual browsers.

As shown in the variable scope table above, a cookie is available from a specific browser to a specific server across sessions. This means that the cookie variables that you set in an individual browser can be referenced in different application pages.

For example, you may create a cookie and send it back to the browser during a user's login. That way, you can reference the cookie to pass specific data from page to page and each time the browser returns to the application. This is extremely important to site personalization strategies.

Cookies are characterized as:

Create cookies using either the CFCOOKIE tag or the CFSET tag on an application page. When you create a cookie:

Note Note:Use Secure Sockets Layer (SSL), to exchange cookies securely.

Setting Cookies example

The code below creates the cookie variable, User_ID, assigns it a numeric value of 2344, and sets it to expire after 100 days:


For more information on CFCOOKIE, see the CFML Language Reference for ColdFusion Express.

Referencing Cookies

Once a server stores a cookie in a browser, that cookie is automatically sent back to the server each time a page is requested by that browser. This means that you can reference that cookie as needed throughout application pages on that server.

Reference cookie variables anywhere on a page to use their values. As with any variable that you reference:

Note Note:Because cookies need to be set by the server before you can use them, always test whether a cookie exists before you use it in an application page.

Always prefix the cookie variable when referencing it.

Referencing Cookies example

For example, you may output a cookie's current value within another CFML tag so that you can use its value to perform conditional processing. The code below outputs the current value of the User_ID.