【问题标题】:Delete multiple dynamically named cookies in Openresty Lua在 Openresty Lua 中删除多个动态命名的 cookie
【发布时间】:2016-01-12 20:20:18
【问题描述】:

以下代码在 Openresty lua 中运行良好

ngx.header["Set-Cookie"] = {
   'test1=; expires=Thu, Jan 01 1970 00:00:00 UTC; domain=test.com;',
   'test2=; expires=Thu, Jan 01 1970 00:00:00 UTC; domain=test.com;'
}

虽然在尝试使 cookie 名称动态化时,它不起作用:

local cookies = {}
local args = {'test1', 'test2'}

for i=1, #args do
  cookies[i] = args[i] .. '=; expires=Thu, Jan 01 1970 00:00:00 UTC; domain=test.com;'
end

ngx.header["Set-Cookie"] = cookies

甚至尝试过使用 table.insert:

local cookies = {}
local args = {'test1', 'test2'}

for i=1, #args do
  table.insert(cookies, args[i] .. '=; expires=Thu, Jan 01 1970 00:00:00 UTC; domain=test.com;')
end

ngx.header["Set-Cookie"] = cookies

将变量分配给ngx.header["Set-Cookie"]似乎存在问题

【问题讨论】:

    标签: cookies lua openresty


    【解决方案1】:

    使用ngx.header.set_cookie 代替ngx.header["Set-Cookie"] 有效。下面的代码现在可以工作了:

    local cookies = {}
    local args = {'test1', 'test2'}
    
    for i=1, #args do
      cookies[i] = args[i] .. '=; expires=Thu, Jan 01 1970 00:00:00 UTC; domain=test.com;'
    end
    
    ngx.header.set_cookie = cookies
    

    【讨论】:

      猜你喜欢
      • 2019-09-18
      • 1970-01-01
      • 2021-06-08
      • 2020-12-20
      • 1970-01-01
      • 2016-09-17
      • 2017-05-30
      • 2020-12-03
      • 1970-01-01
      相关资源
      最近更新 更多