Skip to content

how to get and set cookies? #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
luoqi opened this issue Mar 2, 2011 · 14 comments
Closed

how to get and set cookies? #19

luoqi opened this issue Mar 2, 2011 · 14 comments

Comments

@luoqi
Copy link

luoqi commented Mar 2, 2011

please!

@agentzh
Copy link
Member

agentzh commented Mar 2, 2011

To get cookie "Foo":

local cookie_value = ngx.var.cookie_Foo

To write a cookie Foo:

ngx.header['Set-Cookie'] = 'Foo=abc; path=/'

@luoqi
Copy link
Author

luoqi commented Mar 2, 2011

thank you!

@mvreg
Copy link

mvreg commented May 29, 2011

I have dynamic cookie names, that are defined in nginx-Lua.
local cookie_name="UID2"

How do I read it? I tryed:
local cookie_value = ngx.var[cookie_name]

but it fails.

Any other way?

@agentzh
Copy link
Member

agentzh commented May 30, 2011

You're referring to the $UID2 nginx variable in your Lua code. What you really need is $cookie_UID2:

local cookie_name = "UID2"
local var_name = "cookie_" .. cookie_name
local cookie_value = ngx.var[var_name]

@gauravt
Copy link

gauravt commented Jun 21, 2013

Can we also set an expiry time to it?

@agentzh
Copy link
Member

agentzh commented Jun 21, 2013

Sure!

local expires = 3600 * 24  -- 1 day
ngx.header["Set-Cookie"] = "session=blah; Path=/; Expires=" .. ngx.cookie_time(ngx.time() + expires)

See the wikipedia page for more details on the Set-Cookie response header format:

http://en.wikipedia.org/wiki/HTTP_cookie

@gauravt
Copy link

gauravt commented Jun 25, 2013

Thanks Yichun,
Its working, But when I'm trying to add two cookies together inside a if loop it simply ignores one cookie:
if name== 'new'
local expires = 3600;
ngx.header["Set-Cookie"] = "redirected_from_success=YES; path=/; domain=.freecultr.com; Expires=" .. ngx.cookie_time(ngx.time() +expires); <----THIS Doesn't Works

ngx.header["Set-Cookie"] = "success_rendered=YES; path=/; domain=.freecultr.com; Expires=" .. ngx.cookie_time(ngx.time()+60) <----THIS Works
end
Note: These cookies exists before its overwritten by lua.

@agentzh
Copy link
Member

agentzh commented Jun 25, 2013

@gauravt Assignment to ngx.header.HEADER always overwrites existing HEADER headers. This is by design.

To set multiple headers with the same name, you should assign a Lua array-like table to ngx.header.HEADER, as in

ngx.header["Set-Cookie"] = { cookie1, cookie2, cookie3 }

You can collect all the cookies in your Lua loop in a Lua table, and assign it after the loop in a single run.

@gauravt
Copy link

gauravt commented Jun 28, 2013

Thanks!!

@ghost
Copy link

ghost commented Aug 25, 2014

How to get all the cookies?

@agentzh
Copy link
Member

agentzh commented Aug 25, 2014

@Octolus
Copy link

Octolus commented Jun 4, 2015

I am in need of similar.

I want to check if cookie exist, if it doesn't I want to set it. How?

@agentzh
Copy link
Member

agentzh commented Jun 13, 2015

@TechPulse I think you can just use the builtin nginx variable $cookie_NAME documented below:

http://nginx.org/en/docs/http/ngx_http_core_module.html#var_cookie_

In Lua, you can just test ngx.var.cookie_blah if you want to see if the cookie named "blah" present in the request.

@Fr3DBr
Copy link

Fr3DBr commented Jan 14, 2017

After using ngx.header["Set-Cookie"] in rewrite_by_lua, the Content-Type: header is corrupted, not sure why, but it happens, here's the output:

Date: Sat, 14 Jan 2017 11:47:28 GMT
Content-Type: text/html^@^@^@^@^S�D^@^@^@^@^@��D
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding

The code used to produce this was:

               cname1="COK1_"
               cname2="COK2_"
               cval1="asdjASDJasjdasdjasdiewqeqwoweaksdk1"
               cval2="09as9d0as9d0912k123k12k3aid9asd91#"
               local cookie1    = cname1 .. "=" .. cval1 .. "; Path=/; Expires=" .. ngx.cookie_time(ngx.time() + expire)
                local cookie2    = cname2 .. "=" .. cval2 .. "; Path=/; Expires=" .. ngx.cookie_time(ngx.time() + expire)
                ngx.header["Set-Cookie"] = { cookie1, cookie2 }

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants