Challenge - 5 Problems
Cookie 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 that sets and reads a cookie?
Consider the following PHP code snippet that sets a cookie and then tries to read it immediately. What will be the output?
PHP
<?php setcookie('user', 'Alice', time() + 3600); if (isset($_COOKIE['user'])) { echo $_COOKIE['user']; } else { echo 'No cookie found'; } ?>
Attempts:
2 left
💡 Hint
Remember that cookies set by setcookie() are not available until the next page load.
✗ Incorrect
The setcookie() function sends a cookie header to the browser, but the $_COOKIE superglobal is not updated until the next request. So immediately after setting, $_COOKIE['user'] is not set, resulting in 'No cookie found'.
🧠 Conceptual
intermediate1:00remaining
Which PHP function is used to set a cookie?
In PHP, which function is used to send a cookie to the browser?
Attempts:
2 left
💡 Hint
The function name starts with 'set'.
✗ Incorrect
The correct function to set a cookie in PHP is setcookie(). Other options are invalid function names.
❓ Predict Output
advanced1:30remaining
What will be the output of this PHP code reading a cookie?
Given the following PHP code, what will be the output if the cookie 'session' is set to 'xyz123'?
PHP
<?php if (!empty($_COOKIE['session'])) { echo 'Session ID: ' . $_COOKIE['session']; } else { echo 'No session cookie'; } ?>
Attempts:
2 left
💡 Hint
Check if the cookie exists and is not empty.
✗ Incorrect
If the cookie 'session' is set to 'xyz123', the condition !empty($_COOKIE['session']) is true, so it prints 'Session ID: xyz123'.
🔧 Debug
advanced2:00remaining
Why does this PHP code fail to delete a cookie?
Examine the code below. It tries to delete a cookie named 'theme' but fails. What is the reason?
PHP
<?php setcookie('theme', '', time() + 3600); if (!isset($_COOKIE['theme'])) { echo 'Cookie deleted'; } else { echo 'Cookie still exists'; } ?>
Attempts:
2 left
💡 Hint
To delete a cookie, you must expire it immediately.
✗ Incorrect
To delete a cookie, you must set its expiration time to a past time. Setting it to time() + 3600 sets it to expire in the future, so it remains.
🚀 Application
expert2:30remaining
How many cookies will be sent to the browser after this PHP script runs?
Consider this PHP script that sets multiple cookies. How many cookies will the browser receive?
PHP
<?php setcookie('a', '1', time() + 3600); setcookie('b', '2', time() + 3600, '/path1'); setcookie('c', '3', time() + 3600, '/path2'); setcookie('a', '4', time() + 3600, '/path1'); ?>
Attempts:
2 left
💡 Hint
Cookies with the same name but different paths are treated as separate cookies.
✗ Incorrect
The script issues four setcookie() calls, each producing a distinct Set-Cookie header. The two 'a' cookies differ by path (default '/' vs '/path1'), making all four unique: 'a' (path '/'), 'b' (path '/path1'), 'c' (path '/path2'), 'a' (path '/path1'). Thus, 4 cookies are sent.