2010-06-17 46 views

回答

10

<cfscript>等效于<cfcookie>仅提供直接分配Cookie范围内存变量。您不能使用直接分配来设置存储在用户系统上的持久性cookie。所以如果你想使用脚本CFML设置永久cookie,你将不得不编写一个包装函数。

+0

是啊,谢谢...什么我想过和已经做了。 – noobsaibot 2010-06-17 09:32:00

8

我写了这个UDF。注意它只有CFO,所以你想在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#"> 
2

CF9.0.1没有CFCookie CFSCRIPT等价的,但如果你想使用CFFUNCTION,你仍然可以做到这一点,除了为CF6,7加中HTTPOnly支持,8 & 9.你会能够创建一个UDF,但它只是不会在CFSCRIPT格式

有关于如何在这里做它(源代码)良好的书面记录(没有什么大的损失。): http://www.modernsignal.com/coldfusionhttponlycookie (我不不知道为什么这在CFLib.org还没有。)

我最初遇到的一个问题使用CFHEADER创建cookie的原因是它允许混合大小写的名称,并且ColdFusion只能读取,更新删除大写字母的cookie。

要测试/审查cookies在Firefox和确认截止日期(或中HTTPOnly设置),安装FireCookie Firebug扩展: http://www.softwareishard.com/blog/firecookie/

相关问题