[mod_python] Sessions and Firefox

Sander Voerman 54nd3r at nerim.net
Sun May 15 12:58:04 EDT 2005


Hi all,

I have a strange bug in Session, that only occurs when visiting the  
page with Firefox...

The problem is that, while all other variables are saved into the  
session, the variable sess['allofthem'] isn't saved when the page is  
visited with Firefox. It looks like an initialisation bug, because  
when the key 'allofthem' has been set by visiting the page with  
another browser, everything works flawless..

The goal of the script is to use the session id in the URL, to  
prevent all sorts of cross-domain errors in IE...

The script below can be tested live at: http://python.54nd3r.com/page? 
a=1&b=2
Click refresh to see what variables are saved and which aren't.

Does anyone else have any problems with Sessions and Firefox?

Thanks for all your time,

Sander

Demo script:
#############################################

from mod_python import apache, util, Session

def handler(req):
         """ Test program to illustrate strange Session and Firefox  
bug """
         ### Initialize
         req.content_type = "text/html"
         form = util.FieldStorage(req)
         sess = 0;

         ### Get the session ID from the GET url and initialize the  
session
         sid = form.get('session',None)
         if sid:
                 sess=Session.Session(req, sid, timeout=24*60*60)
         else:
                 sess=Session.Session(req, timeout=24*60*60)

         ### Format the params and values from the GET url
         formstrnew = formstr = " ".join(["%s:%s" % (k, form[k]) for  
k in form.keys()])

         ### Old session variables
         sessionvarsold = ";".join(["%s=%s" % (k, sess[k]) for k in  
sess.keys()])

         if sess.is_new():
                 ### The session is new
                 sess['allofthem'] = formstr
                 req.write("New session<br>")
         elif (sess.has_key('allofthem')):
                 ### Read the former query
                 formstr = sess['allofthem']
                 sess['allofthem'] = formstrnew
                 req.write("Old session<br>")

         ### Put all the submitted values into Session
         for k in form.keys():
                 sess[k] = form[k]

         ### Put all the values stored in Session into a string
         sessionvars = ";".join(["%s=%s" % (k, sess[k]) for k in  
sess.keys()])

         ### Save the session
         sess.save()

         refresh_url = 'http://python.54nd3r.com/page?'
         refresh_url += '&%s' % "&".join(["%s=%s" % (k, form[k]) for  
k in form.keys()])
         if not form.has_key('session'):
                 refresh_url += '&session=%s' % sess.id()

         ### Print the values stored in session

         req.write("Present in previous session  : %s <br>" % formstr)
         req.write("All values in the previous session : %s <br>" %  
sessionvarsold)
         req.write("Put into the current session : %s <br>" % sess 
['allofthem'])
         req.write("All values in the current session : %s <br>" %  
sessionvars)
         req.write("Session id : %s<br>" % sess.id())
         req.write('<a href="%s">refresh</a>' % refresh_url)

         return apache.OK


More information about the Mod_python mailing list