Challenge - 5 Problems
Cookie Security Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code regarding cookie expiration?
Consider the following PHP code snippet that sets a cookie. What will be the expiration time of the cookie relative to the current time?
PHP
<?php setcookie('user', 'Alice', time() + 3600); echo $_COOKIE['user'] ?? 'No cookie'; ?>
Attempts:
2 left
💡 Hint
Remember that time() returns the current Unix timestamp in seconds.
✗ Incorrect
The third argument to setcookie is the expiration timestamp. Using time() + 3600 sets the cookie to expire 3600 seconds (1 hour) from now.
❓ Predict Output
intermediate2:00remaining
What will this PHP code output regarding cookie security flags?
Look at this PHP code that sets a cookie with security flags. What will be the effect of the 'secure' and 'httponly' flags?
PHP
<?php setcookie('session', 'abc123', time() + 600, '/', '', true, true); echo 'Cookie set'; ?>
Attempts:
2 left
💡 Hint
The last two boolean arguments in setcookie control 'secure' and 'httponly' flags.
✗ Incorrect
The 'secure' flag (true) means the cookie is sent only over HTTPS. The 'httponly' flag (true) means JavaScript cannot access the cookie.
🔧 Debug
advanced2:00remaining
Why does this PHP cookie not expire as expected?
This PHP code tries to set a cookie that expires in 10 seconds, but the cookie never expires. What is the problem?
PHP
<?php setcookie('temp', 'value', 10); ?>
Attempts:
2 left
💡 Hint
The expiration time must be a Unix timestamp in the future.
✗ Incorrect
The third argument to setcookie must be a Unix timestamp. Using 10 means the cookie expires at 10 seconds after 1970, which is in the past, so the cookie is deleted immediately.
🧠 Conceptual
advanced1:30remaining
Which cookie attribute improves security by restricting cookie access to same-site requests?
Which cookie attribute helps prevent cross-site request forgery (CSRF) by restricting cookie sending to same-site requests only?
Attempts:
2 left
💡 Hint
This attribute controls when cookies are sent with cross-site requests.
✗ Incorrect
The SameSite attribute restricts cookies to be sent only with same-site requests, helping prevent CSRF attacks.
❓ Predict Output
expert2:30remaining
What is the output of this PHP code regarding cookie deletion?
This PHP code attempts to delete a cookie. What will be the output and effect?
PHP
<?php setcookie('user', '', time() - 3600); if (!isset($_COOKIE['user'])) { echo 'Cookie deleted'; } else { echo 'Cookie still exists'; } ?>
Attempts:
2 left
💡 Hint
Remember that $_COOKIE reflects the cookies sent by the browser in the current request.
✗ Incorrect
setcookie() sends a header to delete the cookie, but $_COOKIE is not updated until the next request, so the cookie still appears set in the current script.