How is a cookie dynamically set based on user input?
How is, for example, a shopping cart cookie set?
Once something is added to the cart it "creates" the cookie but how does the next page know to send a new http header with this new cookie to the client?How is a cookie dynamically set based on user input? Is this functionality dependent on anything else?
I did see this example, but I am missing something.
Any help is appreciated, thanks.
开发者_开发技巧CGI-C
This is a typical set of steps.
- You submit a HTML form to /add_item. You don't send any Cookie header.
- The script at /add_item looks at your HTTP request.
- It sees that you did not send a Cookie header with your request.
- It creates a session for you. That means a set of variables (e.g. a list of items in your cart) are mapped to a unique identifier for your visit. Sessions really only exist on the server. The cookie is all the browser sees.
- It adds the item you added to your session.
- It then sends the output of the "Item Added!" page with a Set-Cookie header. That header contains the new session identifier.
- You add another item in your cart. This time, you send a Cookie header with the same ID the server gave you before.
- The server sees you already have a session ID so it does not create a new session.
- It adds the new item to your existing session.
- It sends the new "Item Added!" page with the same Set-Cookie header as before.
精彩评论