Cookies

Carsten Haese carsten.haese at gmail.com
Wed Dec 30 10:54:26 EST 2009


Victor Subervi wrote:
> Here again is my code:
> 
> #! /usr/bin/python
> 
> import string
> import cgitb; cgitb.enable()
> import MySQLdb
> import cgi
> import sys,os
> sys.path.append(os.getcwd())
> from login import login
> import datetime, Cookie, random
> from particulars import title
> from templateFrame import top, bottom
> from particulars import myCookie
> import time
> import fpformat
> from sets import Set
> from particulars import ourOptions
> 
> def cart():
>   print '''Content-Type: text/html\r\n
> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
> '''
>   cookie = os.environ.has_key('HTTP_COOKIE')
>   if not cookie:
>     cookie = Cookie.SimpleCookie()
>     cExpires, cPath, cComment, cDomain, cMaxAge, cVersion = myCookie()
>     cookie['lastvisit'] = str(time.time())
>     cookie['lastvisit']['expires'] = cExpires
>     cookie['lastvisit']['path'] = cPath
>     cookie['lastvisit']['comment'] = cComment
>     cookie['lastvisit']['domain'] = cDomain
>     cookie['lastvisit']['max-age'] = cMaxAge
>     cookie['lastvisit']['version'] = cVersion
>     cookie['mycookie'] = 'mycookie'
>     cookieFlag = 'new'
>   else:
>     cookie = Cookie.SimpleCookie(cookie)
>     cookieFlag = 'old'
> #  Don't know what to do with this. It's for when client won't accept
> cookies
> #  sessionDir = os.environ['DOCUMENT_ROOT'] + '/tmp/.session'
> #  session = shelve.open(sessionDir + '/sess_' + sid, writeback=True)
> #  session['lastvisit'] = repr(time.time())
> #  session.close()
>   print cookieFlag
>   cookie.load(cookie)
>   print cookie
>  ...
> 
> Please advise.

Pardon me for not being able to read your mind. The code you're now
posting is significantly more code than your first post included. The
line that I had determined to be missing is in fact not missing, you
just didn't bother to post your actual code.

Now that you've posted your actual code, I can see that the actual
problem is that the line is in the wrong place.

The line in question is <<print cookie>>, and it's responsible for
printing a "Set-Cookie" *HEADER*, which is a fact that at least one of
the tutorials you pointed out has mentioned. You're printing it where it
doesn't belong, in the middle of your HTML output. You need to
reorganize your print statements such that the Set-Cookie header is
printed in the header of your script's output.

--
Carsten Haese
http://informixdb.sourceforge.net




More information about the Python-list mailing list