为什么我不能设置多个Cookie [英] How come I cannot set multiple cookies

查看:131
本文介绍了为什么我不能设置多个Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置多个cookie,但是它不起作用:

I'm trying to set multiple cookies, but it's not working:

if type(ngx.header["Set-Cookie"]) ~= "table" then
    ngx.header["Set-Cookie"] = {}
end
table.insert(ngx.header["Set-Cookie"], "Cookie1=abc; Path=/")
table.insert(ngx.header["Set-Cookie"], "Cookie2=def; Path=/")
table.insert(ngx.header["Set-Cookie"], "Cookie3=ghi; Path=/")

在客户端上,我没有收到任何Cookie.

On the client I do not receive any cookies.

推荐答案

ngx.header["Set-Cookie"]是一个特殊的表,每次修改表时都必须将其重新分配给新表(从表中插入或删除的元素对将发送给客户端的Cookie):

ngx.header["Set-Cookie"] is a special table, and must be reassigned to with a new table every time you modify it (elements inserted or removed from it have no effect on the cookies that will be sent to the client):

if type(ngx.header["Set-Cookie"]) == "table" then
    ngx.header["Set-Cookie"] = { "AnotherCookieValue=abc; Path=/", unpack(ngx.header["Set-Cookie"]) }
else
    ngx.header["Set-Cookie"] = { "AnotherCookieValue=abc; Path=/", ngx.header["Set-Cookie"] }
end

这篇关于为什么我不能设置多个Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆