Comments (12)
First of all don't call open
if you have already called start
. It goes like this:
- new
- open (this will call new if needed)
- start (this will call open (and new) if needed)
from lua-resty-session.
Your usage is totally wrong. Why not just:
local session = require "resty.session".start {
secret = "623q4hR325t36VsCD3g567922IC0073T",
cookie = { persistent = true, lifetime = 280 }
}
if session.present then
args.sessionids=session.data.name
args.testsession=1
args.datasession=session.data
local ret=rpccall(red, ' LogininfofromSession', 100, args)
else
-- what are the args here?
local ret=rpccall(red, 'LoigininfofromPasswordandUsername', 100, args)
if ret== "yes" then
session.data.name = "OpenResty Fan"
session:save()
end
end
from lua-resty-session.
To get session id in JS you need to disable http-only cookies (which you really shouldn't):
local session = require "resty.session".start {
secret = "623q4hR325t36VsCD3g567922IC0073T",
cookie = {
persistent = true,
lifetime = 280,
httponly = false
}
}
It is all documented here:
https://github.com/bungle/lua-resty-session#boolean-sessioncookiehttponly
from lua-resty-session.
thank u so much for ur reply, args are arguments get from nginx headers.I try your code,but it seems is not right, When printing session.data, session.data.name ,their value are nil ,they are empty 。But if I added session.open() in the else part, somtimes the session.data.name are
nil or "OpenResty Fan"
`
if session.present then
----------------------------------------- here how can i verify the session is from my client?
args.sessionids=session.data.name
args.testsession=1
args.datasession=session.data
local ret=rpccall(red, ' LogininfofromSession', 100, args)
else
-- what are the args here?
local ret=rpccall(red, 'LoigininfofromPasswordandUsername', 100, args)
if ret== "yes" then
--------- if i add this sentence , it sometimes work
session.open()
---------------------------new added
session.data.name = "OpenResty Fan"
session:save()
end
end
`
can u help me ?why sometimes the session data are empty ,the session is not expired
from lua-resty-session.
I need your full code. Try to make a minimal example that I can try out.
from lua-resty-session.
hello @bungle ,thanks so much,here are my codes
local args = ngx.req.get_uri_args()
ngx.req.read_body()
local h = ngx.req.get_headers()
local f = io.open(ngx.req.get_body_file(), 'rb')
f:close()
local username=args.usr
local passwd=args.passwd
local session = require "resty.session".start{
secret = "623q4hR325t36VsCD3g567922IC0073T",
cookie = { persistent = true, lifetime = 20 } }
if session.present then
args.sessionids=session.data.name
----------- I only want to verify whether the session is from my client
ngx.say(session.data.name)
args.testsession=1
args.datasession=session.data
local ret=rpccall(red, 'LogininfoSession', 100, args)
else
local ret=rpccall(red, 'LogininfoUsePasswd', 100, args)
if(ret==1)then
------------login success i want save the session
session.open()
----------if open function is not here the session data is always nil values why?
session.data.usr = tostring(username)
session.data.passwd = tostring(passwd)
session.data.name = 'OpenResty Fan'
ngx.say(session.data.name)
session:save()
end
end
--ret='yes'
ngx.say(json.encode(ret))
from lua-resty-session.
@bungle ,Are these codes not enough,i can explain where you dont understand,thanks!
from lua-resty-session.
yes, I will look at it tomorrow.
from lua-resty-session.
Ok, I have now looked at this. The problem is basically this:
attempt to set ngx.header.HEADER after sending out response headers,
This code works correctly (I removed all the extra stuff from there that has nothing to do with this issue):
location /test {
content_by_lua_block {
local session = require "resty.session".start {
secret = "623q4hR325t36VsCD3g567922IC0073T",
cookie = {
persistent = true
}
}
if session.present then
ngx.log(ngx.ERR, session.data.name)
else
session.data.name = 'OpenResty Fan'
ngx.log(ngx.ERR, session.data.name)
session:save()
end
}
}
The problem is THESE lines in your code:
ngx.say(session.data.name)
session:save()
See, you send body content there (and also see how I replaced ngx.say
with ngx.log
above). That means you cannot send headers anymore, e.g. a session cookie. If you change this to this:
session:save()
ngx.say(session.data.name)
Your code will work just fine.
I can make extra check in resty.session
to check if headers have already been sent before trying to set the cookie. Raise an error in that case.
from lua-resty-session.
I just released 2.12 that return nil, error on session:start() etc. whenever you try to send cookie if headers where already sent.
from lua-resty-session.
@andyhx, please close this if this is not an issue anymore.
from lua-resty-session.
hi,bungle, I am sorry to reply u late ,it works ,thank u so much
from lua-resty-session.
Related Issues (20)
- Chunked cookies not cleared properly when session is modified and saved again HOT 2
- Chunked cookies are not expired when session shrinks, leaving unwanted data on client HOT 4
- Detect if decryption of cookie failed HOT 1
- Questions about session expiry HOT 2
- Is it possible to specify session id to delete a session? HOT 5
- The session secret and the worker processes HOT 1
- redis get session id / retrieve & decode session content
- Inconsistency in session.start() return values HOT 1
- Session save have no effect after output ?
- prob setting a compressor (zlib) HOT 3
- module 'lua_pack' not found HOT 4
- Session `audience` and `subject` semantics clarifications HOT 2
- Question about `rolling_timeout` HOT 4
- Random SSL error HOT 6
- Does not match state restored from session
- DSHM useless call to setmetadata when pool is specified HOT 1
- What determines session cookie size? HOT 3
- Support etcd as storage HOT 1
- New session cannot be destroyed HOT 7
- undefined symbol: FIPS_mode HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lua-resty-session.