Cookie use: saving personal details

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

Cookie use: saving personal details

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A website remembers your name and greets you with 'Welcome back, Sam!' on your next visit. What has it used?
A A cookie that saved your personal details
B A DNS server that saved your name
2. Which is an example of a cookie saving personal details?
A The page loads faster because the server has more RAM
B Your address is filled in automatically on a checkout form
3. Which type of cookie suits saving your name and address for next time?
A Persistent cookie
B Session cookie
4. Why is a session cookie a poor choice for remembering your name for next week?
A It can only store numbers, not text
B It is deleted when the browser is closed
5. What is the benefit of a cookie that saves your personal details in a sign-up form?
A You do not need to type the same details again
B The website is changed from HTTP to HTTPS

Match the Pairs

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

Left columnRight column
1Set-Cookie: name=Aisha; Max-Age=2592000 AA session cookie is wrong for saving personal details, because it is deleted when the browser closes and the name is lost before the next visit. A persistent cookie is needed.
2<input type="text" name="address" autocomplete="street-address"> BA form box the browser can autofill with a saved address from an earlier visit
3A site greets a returning visitor by name using personal details saved in a cookie CSet-Cookie: address=12 Oak Road; Expires=Fri, 01 Jan 2027 00:00:00 GMT
4A persistent cookie suits saving personal details because it lasts after the browser is closed DThe browser sends the cookie back to the web server, which reads the saved details to fill in forms or greet the user. The cookie does not replace the server.
5Set-Cookie: name=Aisha (session cookie, deleted when the browser closes) ESaves the user's name in a persistent cookie for 30 days so the site can greet her on later visits
6The cookie stores the user's name, so the web server does not need to see it again Fprint("Welcome back, " + cookie_name)

Definitions

cookie personal details

Fill in the Blanks

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

1.
__________: name=Sam; Max-Age=2592000
2.
Set-Cookie: name=Sam; _______=2592000
3.
IF savedName __ "" THEN OUTPUT "Welcome back, " + savedName ENDIF
4.
document.______ = "name=Sam; max-age=86400";
5.
IF savedAddress <> "" THEN addressBox ← ____________ ENDIF

Spot the Error

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

Question 1
A name ← INPUT("Your name")
B SAVE name IN COOKIE "user"
C SET COOKIE TYPE persistent
D SET EXPIRY END OF SESSION
Question 2
A n ← READ COOKIE "name"
B IF n <> "" THEN
C OUTPUT "Welcome back " - n
D ENDIF
Question 3
A addr ← READ COOKIE "name"
B SET addressBox TO addr
C SHOW form
Question 4
A name ← INPUT("Your name")
B DELETE COOKIE "user"
C OUTPUT "Saved!"
Question 5
A IF COOKIE "name" IS EMPTY THEN
B FILL nameBox WITH cookie
C ELSE
D SHOW empty form
E ENDIF

Answer Key

Cookie use: saving personal details
Section A: Quick Questions
Q1
A — A cookie that saved your personal details
Q2
B — Your address is filled in automatically on a checkout form
Q3
A — Persistent cookie
Q4
B — It is deleted when the browser is closed
Q5
A — You do not need to type the same details again
Section B: Match the Pairs
Q1
Set-Cookie: name=Aisha; Max-Age=2592000 → Saves the user's name in a persistent cookie for 30 days so the site can greet her on later visits
Q2
<input type="text" name="address" autocomplete="street-address"> → A form box the browser can autofill with a saved address from an earlier visit
Q3
A site greets a returning visitor by name using personal details saved in a cookie → print("Welcome back, " + cookie_name)
Q4
A persistent cookie suits saving personal details because it lasts after the browser is closed → Set-Cookie: address=12 Oak Road; Expires=Fri, 01 Jan 2027 00:00:00 GMT
Q5
Set-Cookie: name=Aisha (session cookie, deleted when the browser closes) → A session cookie is wrong for saving personal details, because it is deleted when the browser closes and the name is lost before the next visit. A persistent cookie is needed.
Q6
The cookie stores the user's name, so the web server does not need to see it again → The browser sends the cookie back to the web server, which reads the saved details to fill in forms or greet the user. The cookie does not replace the server.
Section C: Definitions
Q1
cookie personal details — A cookie can save personal details like your name or address, so forms are autofilled and you are greeted by name on later visits. A persistent cookie suits this.
Section D: Fill in the Blanks
Q1
Set-Cookie: name=Sam; Max-Age=2592000
Q2
Set-Cookie: name=Sam; Max-Age=2592000
Q3
IF savedName <> "" THEN OUTPUT "Welcome back, " + savedName ENDIF
Q4
document.cookie = "name=Sam; max-age=86400";
Q5
IF savedAddress <> "" THEN addressBox ← savedAddress ENDIF
Section E: Spot the Error
Q1
D — A persistent cookie must keep the saved name after the browser closes, so it needs a future expiry such as 365 DAYS. Ending at the end of the session would make it a session cookie.
Q2
C — Joining the greeting text to the saved name needs the + (concatenation) operator, not -. It should be OUTPUT "Welcome back " + n.
Q3
A — The address must be read from the cookie that stores the address. Reading the "name" cookie would autofill the wrong detail. It should be READ COOKIE "addr".
Q4
B — The program should save the user's name in a cookie so it can be used on later visits. DELETE would remove the cookie. It should be SAVE name IN COOKIE "user".
Q5
A — The form should only be autofilled when the cookie exists. Testing for an empty cookie reverses the logic. It should be IF COOKIE "name" EXISTS THEN.