views:

156

answers:

2

don't seem to be able to set the expiration date of a cookie within cfscript. any hints? it's coldfusion 9 btw.

+4  A: 

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

Andreas Schuldhaus
yeah, thanks ... that what i've thought and already did.
noobsaibot
+2  A: 

I wrote this UDF. Notice that it httpOnly is CF9 only so you would 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#">
CF Jedi Master