The 'expires' attribute sets the date and time when the cookie will be deleted by the browser. After this time, the cookie is no longer sent with requests.
Click to reveal answer
beginner
What is the purpose of the 'Secure' flag in a cookie?
The 'Secure' flag ensures the cookie is only sent over HTTPS connections, protecting it from being sent over unencrypted HTTP.
Click to reveal answer
intermediate
How does the 'HttpOnly' flag improve cookie security?
The 'HttpOnly' flag prevents JavaScript from accessing the cookie, reducing the risk of cross-site scripting (XSS) attacks stealing the cookie.
Click to reveal answer
intermediate
What is the difference between 'expires' and 'max-age' attributes in cookies?
'expires' sets a fixed date/time for cookie expiration, while 'max-age' sets the lifetime in seconds from the current time. 'max-age' is newer and preferred for relative expiration.
Click to reveal answer
intermediate
Why should you set the 'SameSite' attribute on cookies?
The 'SameSite' attribute restricts how cookies are sent with cross-site requests, helping to prevent cross-site request forgery (CSRF) attacks.
Click to reveal answer
Which cookie attribute ensures the cookie is only sent over HTTPS?
ASecure
BHttpOnly
CSameSite
DExpires
✗ Incorrect
The 'Secure' attribute makes sure the cookie is sent only over HTTPS connections.
What does the 'HttpOnly' flag prevent?
ACross-site request forgery
BCookie expiration
CSending cookie over HTTP
DAccess to the cookie via JavaScript
✗ Incorrect
'HttpOnly' prevents JavaScript from accessing the cookie, protecting it from XSS attacks.
If you want a cookie to expire in 1 hour from now, which attribute is best to use?
Aexpires with a fixed date 1 hour later
Bmax-age set to 3600 seconds
CHttpOnly
DSecure
✗ Incorrect
'max-age' sets the cookie lifetime in seconds from now, so 3600 means 1 hour.
What is the main security benefit of setting 'SameSite' to 'Strict'?
AAllows cookies to be accessed by JavaScript
BMakes cookies expire immediately
CPrevents cookies from being sent with cross-site requests
DEncrypts the cookie data
✗ Incorrect
'SameSite=Strict' blocks cookies from being sent on cross-site requests, reducing CSRF risks.
Which PHP function is used to set a cookie with expiration and security flags?
Asetcookie()
Bcookie_set()
Cset_cookies()
Dcookie()
✗ Incorrect
The PHP function 'setcookie()' is used to create cookies with options like expiration, Secure, and HttpOnly.
Explain how to set a cookie in PHP that expires in 2 hours and is secure and HttpOnly.
Think about the parameters of setcookie() including options array.
You got /4 concepts.
Describe why cookie security flags like Secure, HttpOnly, and SameSite are important.
Consider common web security threats related to cookies.
You got /4 concepts.
Practice
(1/5)
1. What does setting the expires parameter in setcookie() control?
easy
A. How long the cookie stays on the user's browser
B. The name of the cookie
C. The value stored inside the cookie
D. The domain where the cookie is valid
Solution
Step 1: Understand the purpose of the expires parameter
The expires parameter sets the time when the cookie will be deleted by the browser.
Step 2: Relate expires to cookie lifetime
Setting expires controls how long the cookie remains stored on the user's device.
Final Answer:
How long the cookie stays on the user's browser -> Option A
Quick Check:
Cookie expiration = lifetime on browser [OK]
Hint: Expires means cookie lifetime in browser [OK]
Common Mistakes:
Confusing expires with cookie name or value
Thinking expires controls cookie content
Mixing expires with domain or path
2. Which of the following is the correct way to set a cookie with secure and HttpOnly flags in PHP?
easy
A. setcookie('user', 'John', time() + 3600, '/', '', true, false);
B. setcookie('user', 'John', time() + 3600, '/', '', false, true);
C. setcookie('user', 'John', time() + 3600, '/', '', true, true);
D. setcookie('user', 'John', time() + 3600, '/', true, true);
Solution
Step 1: Recall setcookie() parameters order
The parameters are: name, value, expire, path, domain, secure, httponly.
Step 2: Identify correct secure and httponly flags
Setting secure to true means cookie sent only over HTTPS, and httponly true means inaccessible to JavaScript.
The secure parameter must be a boolean (true/false), not a string.
Step 2: Identify the incorrect usage
Here, 'true' is a string, which PHP treats as true but is incorrect usage and can cause unexpected behavior.
Final Answer:
The secure flag is a string instead of boolean -> Option A
Quick Check:
Secure flag must be boolean, not string [OK]
Hint: Use true/false without quotes for flags [OK]
Common Mistakes:
Using 'true' or 'false' as strings
Forgetting to set secure flag for HTTPS
Omitting required parameters
5. You want to set a cookie named auth that lasts exactly 2 hours, is only sent over HTTPS, and cannot be accessed by JavaScript. Which of the following is the best way to do this?
hard
A. setcookie('auth', 'token', time() + 3600, '/', '', true, false);
B. setcookie('auth', 'token', time() + 7200, '/', '', false, true);
C. setcookie('auth', 'token', time() + 7200, '/', '', true, false);
D. setcookie('auth', 'token', time() + 7200, '/', '', true, true);
Solution
Step 1: Calculate correct expiration time
2 hours = 7200 seconds, so expiration should be time() + 7200.