-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Comments
To get cookie "Foo": local cookie_value = ngx.var.cookie_Foo To write a cookie Foo:
|
thank you! |
I have dynamic cookie names, that are defined in nginx-Lua. How do I read it? I tryed: but it fails. Any other way? |
You're referring to the $UID2 nginx variable in your Lua code. What you really need is $cookie_UID2:
|
Can we also set an expiry time to it? |
Sure!
See the wikipedia page for more details on the Set-Cookie response header format: |
Thanks Yichun, ngx.header["Set-Cookie"] = "success_rendered=YES; path=/; domain=.freecultr.com; Expires=" .. ngx.cookie_time(ngx.time()+60) <----THIS Works |
@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
You can collect all the cookies in your Lua loop in a Lua table, and assign it after the loop in a single run. |
Thanks!! |
How to get all the cookies? |
@DSanthosh See https://github.com/cloudflare/lua-resty-cookie for an example. |
I am in need of similar. I want to check if cookie exist, if it doesn't I want to set it. How? |
@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. |
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:
The code used to produce this was:
|
please!
The text was updated successfully, but these errors were encountered: