[mod_python] session example

Gregory (Grisha) Trubetskoy grisha at modpython.org
Tue Apr 6 10:23:45 EST 2004



On Tue, 6 Apr 2004, John Mudd wrote:

> I may be way off so feel free to correct me but here what I've come to
> believe.  It's necessary to write cookies before any other writes.

As someone explained it later, the first write flushes the headers, so the
cookie must be set before anything is written.

> So you need to save the session (which writes it's cookie) before any
> other writes.

The cookie is set in the __init__ method of the session object, so saving
the session and the cookie are not related.

Also keep in mind that the cookie is set only once - when the session is
new. After that there is no need for it, it's the client who has to send
the cookie to the server every time, the server does not need to "return"
it. So if your session persists over 100 HTTP requests, 1 will have a
cookie header, and 99 will not.

Grisha

>
> John
>
>
> On Tue, 2004-04-06 at 04:13, pascalbarbedor at free.fr wrote:
> > hi
> > thanks for preceeding answer but I cant make it work.
> >
> > what's wrong with this:
> > ---------------------------------------
> > sess=Session.Session(req)
> >
> > if sess.is_new():
> >     req.write(sess.id())
> > else :
> >     req.write('used session %s'%sess.id())
> >
> > sess['login']='toto'
> >
> > req.write("%s"%sess)
> >
> > sess.save()
> > ----------------------------------------
> >
> > shouldn't it display session used on second reload of the page ?
> >
> >
> > thanks
> > _______________________________________________
> > Mod_python mailing list
> > Mod_python at modpython.org
> > http://mailman.modpython.org/mailman/listinfo/mod_python
>
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>


More information about the Mod_python mailing list