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:
20 cookies can be set in a user's browser for each domain. ColdFusion Server reserves two of these cookies, for CFID and CFTOKEN.
Create cookies using either the CFCOOKIE tag or the CFSET tag on an application page. When you create a cookie:
|Note:||Use Secure Sockets Layer (SSL), to exchange cookies securely.|
The code below creates the cookie variable, User_ID, assigns it a numeric value of 2344, and sets it to expire after 100 days:
<CFCOOKIE NAME="User_ID" VALUE="2344" EXPIRES="100">
Optional when creating a cookie. Use a value of now to delete a cookie.
For more information on CFCOOKIE, see the CFML Language Reference for ColdFusion Express.
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:
Always prefix the cookie variable when referencing it.
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.
<CFOUTPUT> #Cookie.User_ID# </CFOUTPUT>