0
0
PHPprogramming~20 mins

How cookies work in PHP - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cookie Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP cookie code?

Consider this PHP script that sets a cookie and then tries to read it immediately:

<?php
setcookie('user', 'Alice', time() + 3600);
if (isset($_COOKIE['user'])) {
    echo 'User is ' . $_COOKIE['user'];
} else {
    echo 'No user cookie found';
}
?>

What will this script output when run for the first time in a browser?

PHP
<?php
setcookie('user', 'Alice', time() + 3600);
if (isset($_COOKIE['user'])) {
    echo 'User is ' . $_COOKIE['user'];
} else {
    echo 'No user cookie found';
}
?>
AUser is Alice
BUser is
CError: Undefined index 'user'
DNo user cookie found
Attempts:
2 left
💡 Hint

Remember that cookies are sent by the browser on the next request, not immediately.

🧠 Conceptual
intermediate
1:00remaining
Which HTTP header is used to set a cookie?

When a server wants to store a cookie on a user's browser, which HTTP response header does it use?

ASet-Cookie
BContent-Type
CAuthorization
DCookie
Attempts:
2 left
💡 Hint

Think about the header sent from server to client to store data.

Predict Output
advanced
2:00remaining
What is the output of this PHP cookie deletion code?

Look at this PHP code that tries to delete a cookie:

<?php
setcookie('session', '', time() - 3600);
if (isset($_COOKIE['session'])) {
    echo 'Session cookie exists';
} else {
    echo 'Session cookie deleted';
}
?>

What will this script output when run immediately after the cookie was set previously?

PHP
<?php
setcookie('session', '', time() - 3600);
if (isset($_COOKIE['session'])) {
    echo 'Session cookie exists';
} else {
    echo 'Session cookie deleted';
}
?>
AError: Cannot delete cookie
BSession cookie exists
CSession cookie deleted
DSession cookie
Attempts:
2 left
💡 Hint

Think about when the cookie is actually removed from $_COOKIE.

🔧 Debug
advanced
1:30remaining
Why does this PHP cookie code cause a warning?

Consider this PHP code snippet:

<?php
setcookie('theme', 'dark');
echo $_COOKIE['theme'];
?>

When run, it sometimes shows a warning: Undefined index: theme. Why?

PHP
<?php
setcookie('theme', 'dark');
echo $_COOKIE['theme'];
?>
ABecause the cookie is not available in $_COOKIE until the next request
BBecause setcookie() does not actually set cookies
CBecause $_COOKIE is read-only and cannot be accessed
DBecause 'theme' is a reserved keyword in PHP
Attempts:
2 left
💡 Hint

Think about when cookies become available in PHP.

🚀 Application
expert
1:30remaining
How many cookies will be sent to the server?

A website sets these cookies with these paths:

  • Cookie A: path=/
  • Cookie B: path=/shop
  • Cookie C: path=/shop/sale

If the browser requests the URL https://example.com/shop/sale/item1, how many cookies will be sent to the server?

A1
B2
C3
D0
Attempts:
2 left
💡 Hint

Cookies are sent if the request path starts with the cookie's path.