Problem dealing with cookies

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fazer

    Problem dealing with cookies

    Hello,

    I have the following code:

    #!/usr/local/bin/python
    import cgi
    import Cookie

    C = Cookie.SimpleCo okie()
    C["moo"] = "f"
    C["moo"]["path"] = "/"
    C["moo"]["expires"] = 60*60*60*60

    #Print the headers
    print C
    print "Content-Type: text/html\n"

    #Print starting html
    print "<html><head><t itle>Cookie</title></head><body>"

    form = cgi.FieldStorag e()

    if C['moo'].value != "f":
    print "I remember you %s" % (C["moo"].value)

    elif form.has_key("n ame") and C["moo"].value == "f":
    print "Thank you %s!" % (form["name"].value)
    C["moo"] = form['name'].value
    else:
    print '<form method="POST" action="ses.py" >'
    print '<input type="text" name="name">'
    print '</form>'

    print "</body></html>"

    I am new in learning Python and I can't get this to work. Basically,
    it asks you to input something and thanks you with the display of what
    you entered.

    It should remember you. SO when you refresh, it should say that it
    remembers you. But it doesn't and shows the input box again.

    What could possibly be wrong here?


    Thanks in advance!

    Fazer
  • John J. Lee

    #2
    Re: Problem dealing with cookies

    faizan@jaredweb .com (Fazer) writes:
    [color=blue]
    > I have the following code:[/color]
    [...][color=blue]
    > C = Cookie.SimpleCo okie()
    > C["moo"] = "f"
    > C["moo"]["path"] = "/"
    > C["moo"]["expires"] = 60*60*60*60
    >
    > #Print the headers
    > print C[/color]

    Here (the print statement), you're setting the client's cookie "moo"
    to "f", no matter what. What you wanted was to put the form's data in
    the cookie, if the request contains form data.

    [color=blue]
    > print "Content-Type: text/html\n"
    >
    > #Print starting html
    > print "<html><head><t itle>Cookie</title></head><body>"
    >
    > form = cgi.FieldStorag e()
    > [...]
    > if C['moo'].value != "f":
    > print "I remember you %s" % (C["moo"].value)[/color]

    You're expecting C to have magically aquired a new value between the
    point where you set it above and this point. How is that supposed to
    happen?

    [color=blue]
    > elif form.has_key("n ame") and C["moo"].value == "f":
    > print "Thank you %s!" % (form["name"].value)
    > C["moo"] = form['name'].value[/color]
    [...]

    More of the same. You're setting C["moo"], but how is the client
    supposed to find out? SimpleCookie isn't quite that magical. :-)

    Remember the way all this gets executed: a request comes in, the web
    server picks the right CGI script based on the request URL, and your
    script sees the HTTP headers (including Cookie) as environment
    variables. You use the headers from the environment (with something
    like C.load(os.envir on["HTTP_COOKI E"]) and forms = cgi.FieldStorag e()
    -- both look in os.environ, one explicitly, the other not), then you
    output some HTTP headers and then some data (usually HTML). Request,
    response. You're trying to sneak several request-response cycles into
    one CGI execution.

    So, you either want a single CGI script and some kind of switch (am I
    looking at the first request, before the user has seen the form, or
    the second, with the submitted form data?), or two CGI scripts (one
    for the first request, one for the second with the form data -- set
    the form's action HTML-attribute to point to that second CGI script).

    Probably, you'll quickly find that life is simpler (for anything
    non-trivial) if you use a web programming framework. Don't be afraid
    of them! Albatross might be a good choice -- designed to be a small
    step up from CGI, and pure Python so easy to install on a server --
    but there are plenty of others.

    In all cases, remember that you have to assume you're dealing not with
    a well-behaved user (no such beast) using a well-behaved browser, but
    an evil hacker crafting evil HTTP headers and data exactly how he
    wants them.


    John

    Comment

    Working...