开发者

how to set expiration date on a cookie in cfscript

开发者 https://www.devze.com 2023-01-03 14:41 出处:网络
don\'t seem to be able to set the expiration date of a cookie within cfscript.开发者_运维百科 any hints? it\'s coldfusion 9 btw.The <cfscript> equivalent to <cfcookie> offers only direct a

don't seem to be able to set the expiration date of a cookie within cfscript.开发者_运维百科 any hints? it's coldfusion 9 btw.


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.


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#">


CF9.0.1 doesn't have a CFCookie cfscript equivalent, but if you want to use CFFUNCTION, you can still do it in addition to adding HTTPOnly support for CF6, 7, 8 & 9. You'll be able to create a UDF, but it just won't be in CFSCRIPT format (no big loss.)

There's a good writeup regarding how to do it here (with source code): http://www.modernsignal.com/coldfusionhttponlycookie (I don't know why this isn't available at CFLib.org yet.)

One issue that I initially encountered creating cookies using CFHEADER was that it allowed mixed-case names and ColdFusion is only capable of reading, updating & deleting cookies with uppercase names.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消