[mod_python] Session.Session(req) not finding saved session

Will Gray graywh at gmail.com
Tue May 1 17:25:56 EDT 2007


On 5/1/07, Will Gray <graywh at gmail.com> wrote:
>
>
>
> On 5/1/07, Jim Gallacher <jpg at jgassociates.ca> wrote:
> >
> > Will Gray wrote:
> > > I'm running Apache 2.2.3 and Mod_Python 3.2.10.  I'm using
> > > Mod_Python.Publisher to handle requests.  When I upgraded Apache from
> > > 2.0 to
> > > 2.2 (and Mod_Python, accordingly), I was no longer able to get the
> > saved
> > > session when a visitor changes pages.  Instead, it creates and returns
> > a
> > > new
> > > session.  I was able to verify that the session was still available
> > and
> > > being saved correctly with the data inside.  Here is a sample of what
> > my
> > > views are doing:
> > >
> > > def login(req):
> > >    sess = Session.Session(req)
> > >    userid = sess.get('uid', None)
> > >    if userid:
> > >        util.redirect(req, 'view')
> > >    ##get username and password from req.form and authenticate against
> > db##
> > >    if uid:
> > >        sess['uid'] = uid
> > >        sess.save()
> > >        util.redirect(req, 'view')
> > >    else:
> > >        ##display the login form##
> > >
> > > def view(req):
> > >    sess = Session.Session(req) # creates a new session instead of
> > finding
> > > the old one
> > >    userid = sess.get('uid', None)
> > >    if not userid:
> > >        util.redirect(req, 'login')
> > >    ##do some db queries and display the page##
> >
> > I don't see an immediate problem with the code snippet, but given the
> > redirecting back and forth I think the potential is there for things to
> > get out of order.
> >
> > Try tracing through your execution path again and make sure the session
> > is indeed being saved at the appropriate time.
> >
> > Also remember that your session instance must be created *before*
> > returning any output to the client. The first call to req.write()
> > flushes the response headers. Once that happens it's too late to create
> > the session object - you can still create it and save it, but the
> > session cookie will not be sent to the client, so you'll end up with at
> > new session for every request. Typically this results in the behaviour
> > you are seeing - the session data gets saved, but you always end up with
> >
> > a new session.
> >
> >
> I've read everything about making sure sessions are created before
> req.write() and know that is not the problem.  The cookie is there in the
> client web browser with the correct "pysid" value for the session id.
>
> Since everything worked fine before upgrading Apache from 2.0 to 2.2 it
> makes me think there is something wrong with that or mod_python.
>

I rearranged my code to more closely match my example above and everything
is fine now.  I did have the redirect in a different place, so that must
have made a big difference.

-- 
Will Gray
Nashville, TN
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20070501/d031f2e6/attachment-0001.html


More information about the Mod_python mailing list