[mod_python] Cookies not persisting

John Raines jrraines at comcast.net
Thu May 10 19:30:32 EDT 2007


Syntax of the Set-Cookie HTTP Response Header

This is the format a CGI script would use to add to the HTTP headers  
a new piece of data which is to be stored by the client for later  
retrieval.
Set-Cookie: NAME=VALUE; expires=DATE;
path=PATH; domain=DOMAIN_NAME; secure
NAME=VALUE
This string is a sequence of characters excluding semi-colon, comma  
and white space. If there is a need to place such data in the name or  
value, some encoding method such as URL style %XX encoding is  
recommended, though no encoding is defined or required.
This is the only required attribute on the Set-Cookie header.

expires=DATE
The expires attribute specifies a date string that defines the valid  
life time of that cookie. Once the expiration date has been reached,  
the cookie will no longer be stored or given out.
The date string is formatted as:
Wdy, DD-Mon-YYYY HH:MM:SS GMT
This is based on RFC 822, RFC 850, RFC 1036, and RFC 1123, with the  
variations that the only legal time zone is GMT and the separators  
between the elements of the date must be dashes.
expires is an optional attribute. If not specified, the cookie will  
expire when the user's session ends.



So if you don't specify, the cookie disappears when the browser quits.


On May 10, 2007, at 1:39 PM, Ryan Kaskel wrote:

> I am trying to fiddle around with AJAX but before I can get to that  
> part I need to make a little test login system will automatically  
> log the user in if a cookie is found. The problem is when I close  
> the browser the cookie is deleted. Everything else should work  
> (when I click the logout button I am still staying logged in  
> because it found the cookie). Does it have something to do with my  
> browser or an attribute I am not setting when I create the cookie.
>
> Thanks,
> Ryan
>
> below is the code:
>
> import time
> from mod_python import util
> from mod_python import Session, Cookie
>
> username = "test"
> password = "pass"
>
> main_page2 = """
> <html>
> <head>
>  <title>Main</title>
> </head>
> <body>
>  Welcome!
>  <p> <a href=\"./logout\">Logout</a> </p>
> </body>
> </html>
> """
>
> login_page = """
> <html>
> <head>
>  <title>Login</title>
> </head>
> <body>
>  <h1>Please login</h1>
>  <form action=\"./login\" method=\"POST\">
>   Username <input type=\"text\" name=\"user\"/> <br/>
>   Password <input type=\"text\" name=\"pass\"/> <br/>
>   Remember me? <input type=\"checkbox\" name=\"remember\"/> <br/>
>   <button type=\"submit\">Login</button>
>  </form>
> </body>
> </html>
> """
> def main(req):
>    req.content_type = 'text/html'
>    main_page = main_page2
>    session = Session.Session(req)
>    cookies = Cookie.get_cookies(req, Cookie.MarshalCookie,  
> secret="example")
>    if cookies.has_key('example1'):
>        cookie = cookies['example1']
>        if type(cookie) is Cookie.MarshalCookie:
>            data = cookie.value
>            main_page = data['user'] + " " + data['passw'] + main_page
>            session['valid'] = 'true'
>            session.save()
>    else:
>        if session.is_new():
>            util.redirect(req,'./login')
>        if session['valid'] != 'true':
>            util.redirect(req,'./login')
>    req.write(main_page)
>
> def login(req):
>    req.content_type = "text/html"
>    if req.method == 'POST':
>        if req.form['user'] == username and req.form['pass'] ==  
> password:
>            session = Session.Session(req)
>            session['valid'] = 'true'
>            session.save()
>            if req.form.has_key('remember') and req.form['remember']:
>                value = {'user': req.form['user'], 'passw': req.form 
> ['pass']}
>                Cookie.add_cookie(req, Cookie.MarshalCookie 
> ('example1', value,'example'), expires=time.time() + 3000000)
>            util.redirect(req,'./main')
>        else:
>            req.write("bad credentials")
>    else:
>        req.write(str(Cookie.get_cookies(req, Cookie.MarshalCookie,  
> secret='example')))
>        req.write(login_page)
>
> def logout(req):
>    req.content_type = "text/html"
>    session = Session.Session(req)
>    if session.has_key('valid'):
>        session.delete()
>    util.redirect(req,'./main')
>
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20070510/4fe9e702/attachment-0001.html


More information about the Mod_python mailing list