ColdFusion CFCookieSeptember 15, 2011
The <cfcookie> tag is used to store simple values up to 4KB (a cookie) to the client. Cookies are great for storing short bits of information to the client such as a username and password combination so that information is remembered when the user returns to your site.
At a minimum, I generally create a cookie using the name, value, and expires attributes like this:
<cfcookie name="TestCookie" value="Test Value" expires="never">
The name attribute assigns the name of the cookie to the client. The value "Test Value" is the value that is stored within TestCookie. And setting "never" to expires means that the cookie will always exist, unless cleared by the user manually. In reality, I believe assigning "never" actually assigns it a date 30 years out from the creation of the cookie. If you wanted to assign an actual expiration date, you can do so using a MM/DD/YY format. Setting expires to "now" is a quick way to delete the cookie. And, you can also assign a number of days until the cookie expires from the date of its creation.
You can now access or output the contents of your cookie using the following example:
There are a few additional attributes that you can pass in a <cfcookie> tag that can also be beneficial:
The "domain" attribute allows you to pass an additional domain if you would like your cookie available on a different server. If you wanted your cookie available on nvisionweb.com, you could pass a value ".nvisionweb.com" using this attribute.
The "path" attribute allows you to create a cookie that is only available within the selected path of the server. Setting a value of "/home/" will not allow you to access the cookie in the root directory, only in the path you assign.
Finally, the "secure" attribute allows you to pass a boolean value that tells the client whether the cookie can only be accessed through a secure protocol (https) or not.
One additional item to note is that the maximum length of each cookie is 4KB. This doesn't just include the value of the cookie, but also the name, expiration date, etc., so make sure you keep that in mind when creating cookies. Since cookies are generally used to store smaller values, this size constraint shouldn't be an issue.
January 28, 2012
Hello! Very good article - thank you! I hope that it will help me in my work. The main thing will never stop! Good luck guys!