Gregory's Blog

Using a dynamic variable to set a dynamic path variable when dropping a cookie

ColdFusion sometimes drives me a bit wonky when I try to set a dynamic variable inside a tag. Most ColdFusion tags support embedded dynamic variables, but quite a few tags don't support this. Probably the most frustrating issue is when you want to use a dynamic variable to extend an application in a sub-folder using two or more application.cfc templates. It seems that every other time I do this I slap my head and think 'Oops! it's time to set up proxy extensions', sigh... but this frustrating issue requires a very long blog entry for a different day. Back to my point...

If you try to use a dynamic variable using a cfcookie tag, you will get the following nasty error:

view plain about
1<cfcookie name="isAdmin" value="true" path="#application.baseUrl#" expires="never">

Attribute validation error for tag CFCOOKIE. It has an invalid attribute combination: expires,name,path,value. Possible combinations are: Required attributes: 'name'. Optional attributes: 'domain,encodevalue,expires,httponly,preservecase,secure,value'. Required attributes: 'domain,name,path'. Optional attributes: 'encodevalue,expires,httponly,preservecase,secure,value'.

To get around this, simply use the cookie scope instead in order to set the dynamic path value.

view plain about
1<!--- Using the cfcookie tag does not work with dynamic vars in the path. --->
2<cfset cookie.isAdmin = { value="true", path="#application.baseUrl#", expires=30 }>

Both cfcookie and the cookie scope do the same thing, send a cookie to the client, but at least the cookie scope allows you to embed dynamic variables for in the path name.

This entry was posted on June 28, 2019 at 12:11 PM and has received 152 views.

There are currently 0 comments. Download attachment.




Your input and contributions are welcomed!

If you have an idea, BlogCfc based code, or a theme that you have built using this site that you want to share, please contribute by making a post here or share it by contacting us! This community can only thrive if we continue to work together.

Images and Photography:

Gregory Alexander either owns the copyright, or has the rights to use, all images and photographs on the site. If an image is not part of the "Gregory's Blog" open sourced distribution package, and instead is part of a personal blog post or a comment, please contact us and the author of the post or comment to obtain permission if you would like to use a personal image or photograph found on this site.

Credits:

Portions of Gregory's Blog are powered on the server side by BlogCfc, an open source blog developed by Raymond Camden. Revitalizing BlogCfc was a part of my orginal inspiration that prompted me to design this site. Some of the major open source contributers to BlogCfc include:

  1. Peter Farrell: the author of 'Lyla Captcha' that is used on this blog.
  2. Pete Freitag: the author of the 'ColdFish' code formatter that is also used on this blog.

Version:

Gregory's Blog Version 1.1 July 14th, 2019.