How to set cookie expiration date in cfscript

don't seem to be able to set the cookie expiration date in cfscript. any clues? this is coldfusion 9 btw.

+7
coldfusion cookies
source share
3 answers

<cfscript> , equivalent to <cfcookie> , offers only the direct assignment of memory variables for cookies only. You cannot use direct assignment to set persistent cookies that are stored on the user system. Therefore, you will have to write a wrapper function if you want to set persistent cookies using only CFML script.

+10
source share

I wrote this UDF. Please note that httpOnly is only CF9, so you want to remove it under CF8.

 <cffunction name="setCookie" access="public" returnType="void" output="false"> <cfargument name="name" type="string" required="true"> <cfargument name="value" type="string" required="false"> <cfargument name="expires" type="any" required="false"> <cfargument name="domain" type="string" required="false"> <cfargument name="httpOnly" type="boolean" required="false"> <cfargument name="path" type="string" required="false"> <cfargument name="secure" type="boolean" required="false"> <cfset var args = {}> <cfset var arg = ""> <cfloop item="arg" collection="#arguments#"> <cfif not isNull(arguments[arg])> <cfset args[arg] = arguments[arg]> </cfif> </cfloop> <cfcookie attributecollection="#args#"> </cffunction> <cfscript> if(!structKeyExists(cookie, "hitcount")) setCookie("hitcount",0); setCookie("hitcount", ++cookie.hitcount); setCookie("foreverknight",createUUID(),"never"); </cfscript> <cfdump var="#cookie#"> 
+8
source share

CF9.0.1 does not have the equivalent of cfscript CFCookie, but if you want to use CFFUNCTION, you can still do this in addition to adding HTTPOnly support for CF6, 7, 8 and 9. You can create UDF, but it just won't be in CFSCRIPT format (without big losses).

There's a good entry on how to do this here (with source code): http://www.modernsignal.com/coldfusionhttponlycookie (I don't know why this is not yet available on CFLib.org.)

One of the problems that I encountered creating cookies using CFHEADER was that it allowed mixed-type names, and ColdFusion is able to read, update and delete cookies with uppercase names.

To check / view cookies in Firefox and confirm the expiration date (or setting HTTPOnly), install the FireCookie Firebug extension: http://www.softwareishard.com/blog/firecookie/

+2
source share

All Articles