开发者

How is a cookie dynamically set based on user input?

开发者 https://www.devze.com 2022-12-19 08:11 出处:网络
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 cli

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.

  1. You submit a HTML form to /add_item. You don't send any Cookie header.
  2. The script at /add_item looks at your HTTP request.
  3. It sees that you did not send a Cookie header with your request.
  4. 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.
  5. It adds the item you added to your session.
  6. It then sends the output of the "Item Added!" page with a Set-Cookie header. That header contains the new session identifier.
  7. You add another item in your cart. This time, you send a Cookie header with the same ID the server gave you before.
  8. The server sees you already have a session ID so it does not create a new session.
  9. It adds the new item to your existing session.
  10. It sends the new "Item Added!" page with the same Set-Cookie header as before.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号