本文介绍了如何在访问某个网页时删除客户端Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我访问某个网页时删除客户端Cookie。我如何做到这一点?即使在使用下面的代码后,我也无法删除客户端Cookie:

 < cfcookie name =GIFT CAT value =expires =NOW/> 
< cfset StructDelete(cookie,'GIFTCAT',false)>


解决方案


$ b

听起来你正在测试cookie状态不正确。您无法在单个http请求中执行此操作。必须将http响应发送回浏览器才能将客户端Cookie实际删除。该更改将反映在下一个 http请求中。





此外,如果您查看ColdFusion文档,它说明 expire =now 不会从Cookie中删除相应的变量活动页面的范围)。所以如果你删除一个cookie,然后转储 cookie 范围在同一页上,删除的cookie仍然会存在。在你的情况下, value 将是一个空字符串。



正确测试行为需要三个请求:


  1. 创建cookie:
    < cfcookie name =GIFT_CATvalue = #now()#/> 。在浏览器中运行脚本GIFT_CAT:




    • 存在于CF cookie 范围

    • 存在于浏览器Cookie



  2. 删除Cookie:< cfcookie name =GIFT_CATvalue =expires =NOW/> 。在浏览器中运行脚本GIFT_CAT:




    • 仍然存在于CF cookie scope(因为 expire 不会在活动页面上删除它)

    • 浏览器Cookie中不存在在收到回应后删除Cookie)



  3. 最后,验证Cookie:< cfdump var =#COOKIE#> 。在浏览器中运行脚本GIFT_CAT:




    • 不存在于CF cookie 范围

    • 在浏览器Cookie中不存在



I am trying to delete a client-side cookie when I access a certain page. How to I do this? Even after using the code below, I'm unable to delete the client side cookie:

<cfcookie name="GIFT CAT" value="" expires="NOW" />
<cfset StructDelete(cookie, 'GIFTCAT', false)>
解决方案

(Expanded from comments)

It sounds like you are testing the cookie status incorrectly. You cannot do this in a single http request. The http response must be sent back to the browser for the client cookie to actually be deleted. That change will be reflected in the next http request.

when i keep the dump to see the result showing empty string

Also, if you review the ColdFusion documentation it states that expire="now" does not delete the corresponding variable [from] the Cookie scope of the active page). So if you delete a cookie, then dump the cookie scope on the same page, the deleted cookie will still exist. In your case the value will be an empty string.

Properly testing the behavior requires three requests:

  1. Create the cookie:<cfcookie name="GIFT_CAT" value="Created cookie at #now()#"/>. After running the script in your browser, "GIFT_CAT":

    • Exists in the CF cookie scope
    • Exists in browser cookies

  2. Delete the cookie: <cfcookie name="GIFT_CAT" value="" expires="NOW" />. After running the script in your browser, "GIFT_CAT":

    • Still exists in the CF cookie scope (because expire does not delete it on the active page)
    • Does NOT exist in browser cookies (because the browser deletes the cookie after receiving the response)

  3. Finally, verify the cookies: <cfdump var="#COOKIE#">. After running the script in your browser, "GIFT_CAT":

    • Does NOT exist in the CF cookie scope
    • Does NOT exist in browser cookies

这篇关于如何在访问某个网页时删除客户端Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 18:12
查看更多