Inserting cookies into a web session

J

John Gordon

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.
 
M

Michael Ströder

John said:
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.
 
J

John Gordon

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.

That looks great!
Ciao, Michael.

Thanks Michael. :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top