On Wed, 25 Apr 2012 15:49:23 -0700 (PDT), varelay...@gmail.com said:

> I'm quite new to web2py and a noob at it. I've been struggling with the 
> difference between cookies and sessions...But I have plenty of questions
> about them and how to use them(in web2py)

I think you should approach them from a different perspective. You are
doing the equivalent of looking at a screwdriver and asking what you can
do with it, whereas you may be better off saying, "How can I replace this
hard disk?". In other words, what problem are you trying to solve?

That said, I'll try to offer some guidance. I suggest you ignore cookies
altogether. Web2py uses them, but YOU don't have to. Think of the session
as a temporary store to allow you to pass variables from one place to
another. Given that HTTP[S] is a stateless protocol, it can only tell you
that a user clicked on a link or typed a URL in the browser, not that they
have put 25 widgets in their shopping cart (that's simplifying it a
little, but hopefully you get the point). 

In your code, you can use the session to store the contents of the
shopping cart (for example). So, when they click on 'buy', you might do
something like:

        if session.cart:
                session.cart.append(dict(item='widget',q=2))
        else:
                session.cart = [dict(item='widget',q=2)]

Later, when they checkout, you can do:

        for orderline in session.cart:
                process(orderline['item'],orderline['q'])

There are neater ways to do this, particularly if you use Storage()
objects rather than dictionaries, but the principle is that you can use
the session to give variables persistence between requests.

> *1. What can you store in them? *

In a session: anything than can be pickled (notable NOT user-defined
objects).

> *2. When are they deleted?*

When you do session.forget()

> *3. Could you use cookies as temporary DB?*

What are you trying to achieve?

Hopefully that gives you some idea...
-- 
"You can have everything in life you want if you help enough other people
get what they want" - Zig Ziglar. 

Who did you help today?

Reply via email to