Coder Social home page Coder Social logo

Comments (12)

bungle avatar bungle commented on August 15, 2024

First of all don't call openif you have already called start. It goes like this:

  1. new
  2. open (this will call new if needed)
  3. start (this will call open (and new) if needed)

from lua-resty-session.

bungle avatar bungle commented on August 15, 2024

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.

bungle avatar bungle commented on August 15, 2024

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.

andyhx avatar andyhx commented on August 15, 2024

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.

bungle avatar bungle commented on August 15, 2024

I need your full code. Try to make a minimal example that I can try out.

from lua-resty-session.

andyhx avatar andyhx commented on August 15, 2024

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.

andyhx avatar andyhx commented on August 15, 2024

@bungle ,Are these codes not enough,i can explain where you dont understand,thanks!

from lua-resty-session.

bungle avatar bungle commented on August 15, 2024

yes, I will look at it tomorrow.

from lua-resty-session.

bungle avatar bungle commented on August 15, 2024

@andyhx,

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.

bungle avatar bungle commented on August 15, 2024

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.

bungle avatar bungle commented on August 15, 2024

@andyhx, please close this if this is not an issue anymore.

from lua-resty-session.

andyhx avatar andyhx commented on August 15, 2024

hi,bungle, I am sorry to reply u late ,it works ,thank u so much

from lua-resty-session.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.