Problem with session variables not being set

3,107 views
Skip to first unread message

Aziz Light

unread,
Apr 15, 2011, 8:42:29 AM4/15/11
to sina...@googlegroups.com
Hello everybody,
I have been trying to use sessions in Sinatra for some time now, but for some reason session variables are not being set. I using sinatra version 1.2.3. Here is the repository with all the code: https://github.com/AzizLight/wiki

All the sinatra specific code is in lib/wiki.rb and I set a session variable on line 42 in that file (https://github.com/AzizLight/wiki/blob/master/lib/wiki.rb#L42). However the value of the session variable doesn't persist. What I mean by that is that session[:user] (which is the value I set) is always nil except right after I set it. So the question is, what am I doing wrong?

Konstantin Haase

unread,
Apr 15, 2011, 8:48:28 AM4/15/11
to sina...@googlegroups.com
Are you by any change using Shotgun? This will restart the server on every request, thereby regenerate the session secret and thus invalidate your sessions. This has been fixed in current master. Simple fix: set the session_secret option.

Konstantin

> --
> You received this message because you are subscribed to the Google Groups "sinatrarb" group.
> To post to this group, send email to sina...@googlegroups.com.
> To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.

Mika Tuupola

unread,
Apr 15, 2011, 8:53:46 AM4/15/11
to sina...@googlegroups.com

On Apr 15, 2011, at 3:42 PM, Aziz Light wrote:

> I have been trying to use sessions in Sinatra for some time now, but for some reason session variables are not being set. I using sinatra version 1.2.3. Here is the repository with all the code: https://github.com/AzizLight/wiki


If you use shotgun in development mode you should set session secret for sessions to work.

set :session_secret, "here be dragons"

--
Mika Tuupola
http://www.appelsiini.net/

Aziz Light

unread,
Apr 15, 2011, 9:30:07 AM4/15/11
to sina...@googlegroups.com
Ah thanks a lot. It works perfectly with the fix if I use Sinatra's sessions but if I use Rack::Session::Pool it doesn't work with shotgun. I can live with that. Once again thanks a lot.

Konstantin Haase

unread,
Apr 15, 2011, 10:21:02 AM4/15/11
to sina...@googlegroups.com
That is because Session::Pool stores the sessions in-process.

On Apr 15, 2011, at 15:30 , Aziz Light wrote:

> Ah thanks a lot. It works perfectly with the fix if I use Sinatra's sessions but if I use Rack::Session::Pool it doesn't work with shotgun. I can live with that. Once again thanks a lot.
>

DAZ

unread,
Apr 19, 2011, 6:45:29 AM4/19/11
to sinatrarb
Just found this and it has explained why Rack Flash was acting up for
me! Thanks.

Do I only need to explicitly set :session_secret in development mode
and does it matter what it is set to?

cheers,

DAZ

Konstantin Haase

unread,
Apr 19, 2011, 7:51:47 AM4/19/11
to sina...@googlegroups.com
If you have more than on app server running, you should always set it manually. However, you should not share it, since people known your sessions secret will be able to forge fake sessions.

Konstantin

Reply all
Reply to author
Forward
0 new messages