Cookie use: holding items in an online shopping cart

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Cookie use: holding items in an online shopping cart

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A student adds a pair of trainers to an online shopping cart, then clicks to another page of the shop. What lets the website remember the trainers are still in the cart?
A The web page is automatically saved on the DNS server
B A cookie stored by the browser remembers the cart items
2. Which statement about a shopping cart cookie is correct?
A It keeps the cart contents as the user moves between pages
B It empties the cart every time a new page loads
3. A shop only needs the cart to last for one shopping visit. Which type of cookie suits this best?
A Session cookie
B Persistent cookie
4. A shop wants a customer's cart to still be there when they come back tomorrow. Which type of cookie is needed?
A Persistent cookie
B Session cookie
5. A user adds a book to the cart, closes the browser, and reopens it. The cart is now empty. Which cookie was most likely used?
A A persistent cookie
B A session cookie

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Set-Cookie: cart=pencil,ruler AThe server tells the browser to store a cookie named cart that remembers the items the user added to their shopping cart.
2Set-Cookie: cart=book; Max-Age=604800 BSet-Cookie: cart=shoes; Max-Age=86400
3A session cookie suits one shopping visit because it is deleted when the browser is closed. CA session cookie is the better choice for one visit, because it is deleted when the browser closes. A persistent cookie would leave the cart stored for the next person to see.
4A persistent cookie keeps a cart for later because it stays stored until its expiry date. DA persistent shopping cart cookie that keeps the cart for 7 days, so the items are still there when the user returns later.
5Page 1: cart=hat. Page 2: no cookie sent, cart is empty ESet-Cookie: cart=shoes
6A persistent cookie is used for a cart on a shared school computer to hold items for one visit FThe browser did not send the cart cookie back to the server on page 2, so the server forgot the item. The cookie must be sent with each request so the cart survives moving between pages.

Definitions

shopping cart cookie

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
__________: cart=item42
2.
Set-Cookie: cart=item42; _______=604800
3.
GET /basket.html HTTP/1.1 ______: cart=item42
4.
Set-Cookie: cart=item42 # no Max-Age: a _______ cookie
5.
Set-Cookie: cart=item42; Max-Age=86400 # __________: cart kept for later visits

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A INPUT item
B cart ← cart + item
C DELETE cart FROM cookie
D GO TO next page
Question 2
A cookie.type ← "session"
B cart ← READ cookie
C cart ← cart + "Shoes"
D WRITE "empty" TO cookie
Question 3
A ON page load:
B cart ← []
C OUTPUT cart
Question 4
A // One shopping visit
B cookie.type ← "persistent"
C lasts ← "browser closed"
Question 5
A // Keep cart for next week
B cookie.type ← "session"
C cart ← READ cookie
D OUTPUT cart

Answer Key

Cookie use: holding items in an online shopping cart
Section A: Quick Questions
Q1
B — A cookie stored by the browser remembers the cart items
Q2
A — It keeps the cart contents as the user moves between pages
Q3
A — Session cookie
Q4
A — Persistent cookie
Q5
B — A session cookie
Section B: Match the Pairs
Q1
Set-Cookie: cart=pencil,ruler → The server tells the browser to store a cookie named cart that remembers the items the user added to their shopping cart.
Q2
Set-Cookie: cart=book; Max-Age=604800 → A persistent shopping cart cookie that keeps the cart for 7 days, so the items are still there when the user returns later.
Q3
A session cookie suits one shopping visit because it is deleted when the browser is closed. → Set-Cookie: cart=shoes
Q4
A persistent cookie keeps a cart for later because it stays stored until its expiry date. → Set-Cookie: cart=shoes; Max-Age=86400
Q5
Page 1: cart=hat. Page 2: no cookie sent, cart is empty → The browser did not send the cart cookie back to the server on page 2, so the server forgot the item. The cookie must be sent with each request so the cart survives moving between pages.
Q6
A persistent cookie is used for a cart on a shared school computer to hold items for one visit → A session cookie is the better choice for one visit, because it is deleted when the browser closes. A persistent cookie would leave the cart stored for the next person to see.
Section C: Definitions
Q1
shopping cart cookie — A cookie that remembers which items a user has added to an online shopping cart, so the cart is not emptied as they move between pages. It can last one visit or be kept for later.
Section D: Fill in the Blanks
Q1
Set-Cookie: cart=item42
Q2
Set-Cookie: cart=item42; Max-Age=604800
Q3
GET /basket.html HTTP/1.1 Cookie: cart=item42
Q4
Set-Cookie: cart=item42 # no Max-Age: a session cookie
Q5
Set-Cookie: cart=item42; Max-Age=86400 # persistent: cart kept for later visits
Section E: Spot the Error
Q1
C — The cart must be stored in the cookie so it survives the move to the next page. Deleting it would empty the cart. It should be STORE cart IN cookie.
Q2
D — Writing "empty" overwrites the cookie and loses the items. The updated cart, including the shoes, should be written back: WRITE cart TO cookie.
Q3
B — Setting the cart to an empty list empties it on every new page. The page should read the saved cart from the cookie: cart ← READ cookie "cart".
Q4
B — A cart for one shopping visit suits a session cookie, which is deleted when the browser closes. A persistent cookie would be kept longer than needed. It should be "session".
Q5
B — A session cookie is deleted when the browser closes, so the cart would be lost. To keep the cart for later, use a persistent cookie: cookie.type ← "persistent".