问题描述
我尝试在我访问某个网页时删除客户端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
将是一个空字符串。
正确测试行为需要三个请求:
-
创建cookie:
< cfcookie name =GIFT_CATvalue = #now()#/>
。在浏览器中运行脚本GIFT_CAT:
- 存在于CF
cookie
范围 - 存在于浏览器Cookie
- 存在于CF
-
删除Cookie:
< cfcookie name =GIFT_CATvalue =expires =NOW/>
。在浏览器中运行脚本GIFT_CAT:
- 仍然存在于CF
cookie
scope(因为expire
不会在活动页面上删除它) - 浏览器Cookie中不存在在收到回应后删除Cookie)
- 仍然存在于CF
-
最后,验证Cookie:
< cfdump var =#COOKIE#>
。在浏览器中运行脚本GIFT_CAT:
- 不存在于CF
cookie
范围 - 在浏览器Cookie中不存在
- 不存在于CF
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:
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
- Exists in the CF
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 (becauseexpire
does not delete it on the active page) - Does NOT exist in browser cookies (because the browser deletes the cookie after receiving the response)
- Still exists in the CF
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
- Does NOT exist in the CF
这篇关于如何在访问某个网页时删除客户端Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!