Session cookies

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

Session cookies

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. When is a session cookie deleted?
A After 30 days
B When the browser is closed
2. Where is a session cookie held while you browse?
A In memory
B Permanently on the web server
3. A session cookie has an expiry date set a year ahead.
A False
B True
4. How long does a session cookie last?
A Across many visits over weeks
B Only for one visit
5. You close your browser, then open it again. What has happened to the session cookie?
A It has been deleted
B It is still stored

Match the Pairs

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

Left columnRight column
1Set-Cookie: basket=3 AA cookie with no expiry date, so it is a session cookie and is deleted when the browser closes
2session cookie BA cookie held in memory that lasts only for one visit and is deleted when the browser is closed
3A session cookie has no expiry date, so it is not kept after the visit ends CSet-Cookie: sessionId=A7F2; Expires=Fri, 01 Jan 2027
4A cookie that is kept after the browser closes has an expiry date, so it is NOT a session cookie DA session cookie is deleted when the browser is closed. Staying logged in the next day needs a persistent cookie.
5A session cookie is saved on the hard drive for 30 days EA session cookie is held temporarily in memory and is not saved for days. A cookie that is kept for 30 days is a persistent cookie.
6A session cookie stays after you close the browser so you are logged in tomorrow FSet-Cookie: sessionId=A7F2

Definitions

session cookie

Fill in the Blanks

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

1.
__________: sid=a1b2c3
2.
IF browserClosed = TRUE THEN ______ sessionCookie ENDIF
3.
IF cookie.expiry = ____ THEN cookieType ← "session" ENDIF
4.
STORE sessionCookie IN ______
5.
WHILE browserOpen = ____ KEEP sessionCookie ENDWHILE

Spot the Error

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

Question 1
A User visits a shop website
B Server sends persistent cookie
C Cookie is held in memory
D User closes the browser
E Cookie is deleted
Question 2
A Set-Cookie: id=7; Max-Age=999
B Browser holds it in memory
C Browser is closed
D Cookie is deleted
Question 3
A Session cookie created
B It has no expiry date
C It lasts for many visits
D Closing browser deletes it
Question 4
A IF browser closed THEN
B SAVE session cookie
C ENDIF
Question 5
A User opens a shopping site
B Server sends a session cookie
C Cookie is stored in memory
D Cookie expires in 30 days
E Browser is closed
F Cookie is deleted

Answer Key

Session cookies
Section A: Quick Questions
Q1
B — When the browser is closed
Q2
A — In memory
Q3
A — False
Q4
B — Only for one visit
Q5
A — It has been deleted
Section B: Match the Pairs
Q1
Set-Cookie: basket=3 → A cookie with no expiry date, so it is a session cookie and is deleted when the browser closes
Q2
session cookie → A cookie held in memory that lasts only for one visit and is deleted when the browser is closed
Q3
A session cookie has no expiry date, so it is not kept after the visit ends → Set-Cookie: sessionId=A7F2
Q4
A cookie that is kept after the browser closes has an expiry date, so it is NOT a session cookie → Set-Cookie: sessionId=A7F2; Expires=Fri, 01 Jan 2027
Q5
A session cookie is saved on the hard drive for 30 days → A session cookie is held temporarily in memory and is not saved for days. A cookie that is kept for 30 days is a persistent cookie.
Q6
A session cookie stays after you close the browser so you are logged in tomorrow → A session cookie is deleted when the browser is closed. Staying logged in the next day needs a persistent cookie.
Section C: Definitions
Q1
session cookie — A cookie stored temporarily in memory while you browse. It has no expiry date, lasts only for one visit and is deleted when the browser is closed.
Section D: Fill in the Blanks
Q1
Set-Cookie: sid=a1b2c3
Q2
IF browserClosed = TRUE THEN DELETE sessionCookie ENDIF
Q3
IF cookie.expiry = NULL THEN cookieType ← "session" ENDIF
Q4
STORE sessionCookie IN MEMORY
Q5
WHILE browserOpen = TRUE KEEP sessionCookie ENDWHILE
Section E: Spot the Error
Q1
B — The cookie is a session cookie, which is temporary. A persistent cookie would be saved with an expiry date and would not be deleted when the browser closes. Line 2 should be 'Server sends a session cookie'.
Q2
A — A session cookie has no expiry date. Adding Max-Age gives it a lifetime, which makes it a persistent cookie. Line 1 should be 'Set-Cookie: id=7'.
Q3
C — A session cookie lasts only for one visit, because it is deleted when the browser is closed. It does not last for many visits. Line 3 should be 'It lasts for one visit'.
Q4
B — Session cookies are deleted when the browser is closed, not saved. Line 2 should be 'DELETE session cookie'.
Q5
D — A session cookie has no expiry date. A cookie that expires after 30 days would be persistent. Line 4 should be 'Cookie has no expiry date'.