Inserting cookies into a web session

Discussion in 'Python' started by John Gordon, Jul 23, 2008.

  1. John Gordon

    John Gordon Guest

    I'm developing a web application that needs a semi-persistent way to
    store information.

    I've looked at some options such as writing entries to a database table
    or creating small textfiles, but I'm not thrilled with anything I've come
    up with so far.

    Then a coworker suggested that I could just insert a cookie into the
    current session, which would store whatever information I wanted. This
    seems like a really good solution. That's what cookies are *for*, right?

    But I've never worked with cookies before. Is there a Python package for
    handling cookies? And are there any back-end requirements? I assume you
    would have to be running a webserver that supports sessions. Do you also
    have to be running some sort of web container or servlet engine?

    Thanks for any suggestions.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Jul 23, 2008
    #1
    1. Advertising

  2. John Gordon wrote:
    > I'm developing a web application that needs a semi-persistent way to
    > store information.
    >
    > I've looked at some options such as writing entries to a database table
    > or creating small textfiles, but I'm not thrilled with anything I've come
    > up with so far.


    What's the problem?

    > Then a coworker suggested that I could just insert a cookie into the
    > current session, which would store whatever information I wanted. This
    > seems like a really good solution. That's what cookies are *for*, right?


    Before using cookies keep in mind that the cookies returned by the
    browser are not trustworthy! You have to validate the values each time.

    > But I've never worked with cookies before. Is there a Python package for
    > handling cookies?


    http://docs.python.org/lib/module-Cookie.html

    > And are there any back-end requirements?


    No.

    > I assume you
    > would have to be running a webserver that supports sessions.


    You can do this all within your web application.

    > Do you also
    > have to be running some sort of web container or servlet engine?


    No.

    Ciao, Michael.
     
    Michael Ströder, Jul 24, 2008
    #2
    1. Advertising

  3. John Gordon

    John Gordon Guest

    In <> =?ISO-8859-1?Q?Michael_Str=F6der?= <> writes:

    > John Gordon wrote:
    > > I'm developing a web application that needs a semi-persistent way to
    > > store information.
    > >
    > > I've looked at some options such as writing entries to a database table
    > > or creating small textfiles, but I'm not thrilled with anything I've come
    > > up with so far.


    > What's the problem?


    The problem is databases and textfiles are too heavyweight. I only need
    the information for the duration of a session; I don't want to have to worry
    about cleaning up the database or textfile afterwards.

    To explain my requirements a bit more, I'm writing a web-based contact
    manager application. The basic contact unit is a person, and I'm currently
    working on the page layout for displaying information relating to a person.

    The top section of the page is always the same: it shows your personal
    information. Name, userid, title, etc.

    But the bottom section of the page can change; it shows one of several
    different kinds of information. It can either display your home and office
    address, or it can show all the ways you can be contacted (phone, email,
    pager, etc), or it can show the times during which you wish to be contacted.

    (I didn't choose this layout; it was all done and approved before I joined
    the project. I just have to implement it.)

    My problem is if the user goes to the top portion of the page and changes
    some of the information there, for example he wants to change his title,
    that information is self-contained in a form and knows nothing about the
    choice the user made of what to show at the bottom of the page.

    Of course, I could add the choice as a hidden form element at the top of
    the page. But that seems kludgy -- that form *shouldn't* have to know.
    And the page layout isn't as simple as I've described here; there are
    actually lots of these little self-contained forms that are unrelated to
    the information at the bottom of the page, and I'd rather not have to
    add a hidden element to all of them.

    Using a cookie seems like an ideal solution. When the session starts,
    I can set the choice to the default setting of "address", and if the user
    ever clicks on "show me my contact methods" or "show me my times", I just
    set the cookie to that choice. I don't have to worry about passing the
    choice around in each of the dozen or so forms that are on the page.

    > Before using cookies keep in mind that the cookies returned by the
    > browser are not trustworthy! You have to validate the values each time.


    I'm not sure it's worth the trouble in my case. I won't be depending on
    the cookie for sensitive information; I'm just using it as a stored setting
    for which kind of information to display on the page.

    > http://docs.python.org/lib/module-Cookie.html


    That looks great!

    > Ciao, Michael.


    Thanks Michael. :)

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Jul 24, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    3
    Views:
    6,539
    Fredrik Lindner
    Nov 6, 2003
  2. geeteshss
    Replies:
    0
    Views:
    419
    geeteshss
    Jun 20, 2007
  3. Teiko21
    Replies:
    0
    Views:
    348
    Teiko21
    May 26, 2008
  4. _Who
    Replies:
    7
    Views:
    2,686
  5. Navillus

    inserting variables into cookies

    Navillus, Apr 6, 2006, in forum: Javascript
    Replies:
    4
    Views:
    104
    Navillus
    Apr 6, 2006
Loading...

Share This Page