如何删除使用通配符域设置的rails中的cookie:
cookies[:foo] = {:value => 'bar', :domain => '.acme.com'}
Run Code Online (Sandbox Code Playgroud)
按照文档,您可以:
cookies.delete :foo
Run Code Online (Sandbox Code Playgroud)
日志说
Cookie set: foo=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT
Run Code Online (Sandbox Code Playgroud)
请注意,域名丢失了(似乎所有内容都使用默认的参数).尊重RFC,当然cookie还在那里,浏览器 - > ctrl/ cmd- L- >
javascript:alert(document.cookie);
Run Code Online (Sandbox Code Playgroud)
瞧!
问:删除这样一个cookie的"正确"方法是什么?
Jor*_*ter 20
也可以在删除时传递:domain.这是该方法的来源:
# Removes the cookie on the client machine by setting the value to an empty string
# and setting its expiration date into the past. Like []=, you can pass in an options
# hash to delete cookies with extra data such as a +path+.
def delete(name, options = {})
options.stringify_keys!
set_cookie(options.merge("name" => name.to_s, "value" => "", "expires" => Time.at(0)))
end
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它只是设置一个空cookie,其中包含您给出的名称,设置为在1969年到期,并且没有内容.但它确实合并了你给出的任何其他选项,所以你可以这样做:
cookies.delete :foo, :domain => '.acme.com'
Run Code Online (Sandbox Code Playgroud)
你就定了.
归档时间: |
|
查看次数: |
8179 次 |
最近记录: |