@@ -116,17 +116,54 @@ could be added to this module as well (all contributions are welcomed).
116
116
117
117
### Functions and Methods
118
118
119
- #### table session.start(opts or nil)
120
-
121
- With this function you can start a new session. It will create a new session Lua ` table ` on each call.
122
- Right now you should only start session once per request as calling this function repeatedly will overwrite the previously
123
- started session cookie and session data. This function will return a new session ` table ` as a result. If the session cookie
124
- is supplied with user's HTTP(S) client then this function validates the supplied session cookie. If validation
125
- is successful, the user supplied session data will be used (if not, a new session is generated with empty data).
126
- You may supply optional session configuration variables with ` opts ` argument, but be aware that many of these
127
- will only have effect if the session is a fresh session (i.e. not loaded from user supplied cookie). This function
128
- does also manage session cookie renewing configured with ` $session_cookie_renew ` . E.g. it will send a new cookie
129
- with a new expiration time if the following is met ` session.expires - now < session.cookie.renew ` .
119
+ #### table session.new(opts or nil)
120
+
121
+ With this function you can create a new session table (i.e. the actual session instance). This allows
122
+ you to generate session table first, and set invidual configuration before calling ` session:start() ` .
123
+ You can also pass in ` opts ` Lua ` table ` with the configurations.
124
+
125
+ ``` lua
126
+ local session = require (" resty.session" ).new ()
127
+ -- set the configuration parameters before calling start
128
+ session .cookie .domain = " mydomain.com"
129
+ -- call start before setting session.data parameters
130
+ session :start ()
131
+ session .data .uid = 1
132
+ -- save session and update the cookie to be sent to the client
133
+ session :save ()
134
+ ```
135
+
136
+ This is equivalent to this:
137
+
138
+ ``` lua
139
+ local session = require (" resty.session" ).new { cookie = { domain = " mydomain.com" } }
140
+ session :start ()
141
+ session .data .uid = 1
142
+ session :save ()
143
+ ```
144
+
145
+ As well as with this:
146
+
147
+ ``` lua
148
+ local session = require (" resty.session" ).start { cookie = { domain = " mydomain.com" } }
149
+ session .data .uid = 1
150
+ session :save ()
151
+ ```
152
+
153
+ #### table, boolean session.start(opts or nil)
154
+
155
+ With this function you can start a new session. It will create a new session Lua ` table ` on each call (unless called with
156
+ colon ` : ` as in examples above with ` session.new ` ). Right now you should only start session once per request as calling
157
+ this function repeatedly will overwrite the previously started session cookie and session data. This function will return
158
+ a (new) session ` table ` as a result. If the session cookie is supplied with user's HTTP(S) client then this function
159
+ validates the supplied session cookie. If validation is successful, the user supplied session data will be used
160
+ (if not, a new session is generated with empty data). You may supply optional session configuration variables
161
+ with ` opts ` argument, but be aware that many of these will only have effect if the session is a fresh session
162
+ (i.e. not loaded from user supplied cookie). This function does also manage session cookie renewing configured
163
+ with ` $session_cookie_renew ` . E.g. it will send a new cookie with a new expiration time if the following is
164
+ met ` session.expires - now < session.cookie.renew ` . The second ` boolean ` return argument will be ` true ` if the user
165
+ client send a valid cookie (meaning that session was already started on some earlier request), and ` false ` if the
166
+ new session was created (either because user client didn't send a cookie or that the cookie was not a valid one).
130
167
131
168
``` lua
132
169
local session = require (" resty.session" ).start ()
@@ -136,8 +173,8 @@ local session = require("resty.session").start{ identifier = { length = 32 }}
136
173
137
174
#### boolean session: regenerate (flush or nil)
138
175
139
- This function regenerates a session. It will generate a new session identifier and optionally flush the
140
- session data if ` flush ` argument evaluates ` true ` . It will automatically ` session:save ` which
176
+ This function regenerates a session. It will generate a new session identifier ( ` session.id ` ) and optionally
177
+ flush the session data if ` flush ` argument evaluates ` true ` . It will automatically ` session:save ` which
141
178
means that a new expires flag is set on the cookie, and the data is encrypted with the new parameters. With
142
179
client side sessions (server side sessions are not yet supported) this overwrites the current cookie with
143
180
a new one (but it doesn't invalidate the old one as there is no state held on server side - invalidation
@@ -153,10 +190,11 @@ session:regenerate(true)
153
190
154
191
#### boolean session: save ()
155
192
156
- This function saves the session and sends a new cookie to client (with a new expiration time and encrypted data).
157
- You need to call this function whenever you want to save the changes made to ` session.data ` table. It is
158
- advised that you call this function only once per request (no need to encrypt and set cookie many times).
159
- This function returns a boolean value if everything went as planned (you may assume that it is always the case).
193
+ This function saves the session and sends (not immediate though, as actual sending is handled by Nginx/OpenResty)
194
+ a new cookie to client (with a new expiration time and encrypted data). You need to call this function whenever
195
+ you want to save the changes made to ` session.data ` table. It is advised that you call this function only once
196
+ per request (no need to encrypt and set cookie many times). This function returns a boolean value if everything
197
+ went as planned (you may assume that it is always the case).
160
198
161
199
``` lua
162
200
local session = require (" resty.session" ).start ()
@@ -191,7 +229,7 @@ This can be configured with Nginx `set $session_identifier_length 16;`.
191
229
#### string session.key
192
230
193
231
` session.key ` holds the HMAC key. It is automatically generated. Nginx configuration like
194
- ` set $session_check_ua on; ` , ` set $session_check_scheme on; ` and ` set $session_check_addr on; `
232
+ ` set $session_check_ssi on; ` , ` set $ session_check_ua on;` , ` set $session_check_scheme on; ` and ` set $session_check_addr on; `
195
233
will have effect on the generated key.
196
234
197
235
#### table session.data
@@ -238,8 +276,8 @@ with Nginx `set $session_cookie_renew 600;` (600 seconds is the default value).
238
276
239
277
` session.cookie.lifetime ` holds the cookie lifetime in seconds in the future. By default this is set
240
278
to 3,600 seconds. This can be configured with Nginx ` set $session_cookie_lifetime 3600; ` . This does not
241
- set cookie's expiration time on session only (by default), but it is used if the cookies are configured
242
- persistent with ` session.cookie.persistent ` .
279
+ set cookie's expiration time on session only (by default) cookies , but it is used if the cookies are
280
+ configured persistent with ` session.cookie.persistent == true ` .
243
281
244
282
#### string session.cookie.path
245
283
@@ -311,6 +349,13 @@ that was used when the cookie was originally delivered.
311
349
312
350
## Nginx Configuration Variables
313
351
352
+ You can set default configuration parameters directly from Nginx configuration. It's * IMPORTANT* to understand
353
+ that these are read only once (not on every request), for performance reasons. This is especially important if
354
+ you run multiple sites (with different configurations) on the same Nginx server. You can of course set the common
355
+ parameters on Nginx configuration even on that case. But if you are really supporting multiple site with different
356
+ configurations (e.g. different ` session.secret ` on each site), you should set these in code (see: ` session.new `
357
+ and ` session.start ` ).
358
+
314
359
Here is a list of Nginx configuration variables that you can use to control ` lua-resty-session ` :
315
360
316
361
``` nginx
0 commit comments