Bird
Raised Fist0
PHPprogramming~20 mins

Setting and reading cookies in PHP - Practice Problems & Coding Challenges

Choose your learning style10 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 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';
}
?>
AAlice
BEmpty string
CUndefined index error
DNo cookie found
Attempts:
2 left
💡 Hint
Remember that cookies set by setcookie() are not available until the next page load.
🧠 Conceptual
intermediate
1:00remaining
Which PHP function is used to set a cookie?
In PHP, which function is used to send a cookie to the browser?
Acookie_set()
Bsendcookie()
Csetcookie()
Dcreatecookie()
Attempts:
2 left
💡 Hint
The function name starts with 'set'.
❓ Predict Output
advanced
1: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';
}
?>
ASession ID: xyz123
BNo session cookie
CUndefined index: session
DSession ID:
Attempts:
2 left
💡 Hint
Check if the cookie exists and is not empty.
🔧 Debug
advanced
2: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';
}
?>
Asetcookie() cannot delete cookies
BThe expiration time should be in the past to delete a cookie
CCookie name is incorrect
DCookie value cannot be empty
Attempts:
2 left
💡 Hint
To delete a cookie, you must expire it immediately.
🚀 Application
expert
2: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');
?>
A4
B1
C2
D3
Attempts:
2 left
💡 Hint
Cookies with the same name but different paths are treated as separate cookies.

Practice

(1/5)
1. What is the main purpose of using cookies in PHP?
easy
A. To store small pieces of data on the user's computer
B. To execute server-side scripts
C. To connect to a database
D. To style the webpage

Solution

  1. Step 1: Understand what cookies do

    Cookies save small data on the user's computer to remember information like preferences or login status.
  2. Step 2: Identify the correct purpose

    Among the options, only storing small data on the user's computer matches the purpose of cookies.
  3. Final Answer:

    To store small pieces of data on the user's computer -> Option A
  4. Quick Check:

    Cookies = store small data [OK]
Hint: Cookies save small data on user devices [OK]
Common Mistakes:
  • Confusing cookies with server-side code execution
  • Thinking cookies connect to databases
  • Assuming cookies control webpage style
2. Which of the following is the correct way to set a cookie named user with value John in PHP?
easy
A. setcookie('user', 'John');
B. cookie_set('user', 'John');
C. set_cookie('user', 'John');
D. setcookie(user, John);

Solution

  1. Step 1: Recall the correct function name and syntax

    The PHP function to set cookies is setcookie(), which takes the cookie name and value as strings.
  2. Step 2: Check the options for correct syntax

    Only setcookie('user', 'John'); uses the correct function name and quotes around name and value.
  3. Final Answer:

    setcookie('user', 'John'); -> Option A
  4. Quick Check:

    Use setcookie() with quoted strings [OK]
Hint: Use setcookie() with quotes around name and value [OK]
Common Mistakes:
  • Using wrong function names like cookie_set or set_cookie
  • Not quoting the cookie name and value
  • Confusing cookie setting with reading
3. What will be the output of the following PHP code if the cookie color is set to blue?
<?php
if (isset($_COOKIE['color'])) {
    echo $_COOKIE['color'];
} else {
    echo 'No color set';
}
?>
medium
A. color
B. No color set
C. Error: Undefined index
D. blue

Solution

  1. Step 1: Check if the cookie 'color' exists

    The code uses isset() to check if $_COOKIE['color'] is set. Since the cookie is set to 'blue', this condition is true.
  2. Step 2: Output the cookie value

    Because the cookie exists, the code echoes its value, which is 'blue'.
  3. Final Answer:

    blue -> Option D
  4. Quick Check:

    Cookie exists, so output its value [OK]
Hint: Check isset before reading cookie to avoid errors [OK]
Common Mistakes:
  • Assuming cookie is not set and printing default
  • Not using isset and causing undefined index error
  • Confusing cookie name with its value
4. Identify the error in this PHP code that tries to read a cookie named session:
<?php
echo $_COOKIE['session'];
?>
medium
A. Cookie name should be uppercase
B. Wrong function used to read cookie
C. Missing check if cookie exists before reading
D. Cookies cannot be read in PHP

Solution

  1. Step 1: Understand cookie reading best practice

    Directly accessing $_COOKIE['session'] without checking if it exists can cause an undefined index notice if the cookie is not set.
  2. Step 2: Identify the missing check

    The code should use isset($_COOKIE['session']) before echoing to avoid errors.
  3. Final Answer:

    Missing check if cookie exists before reading -> Option C
  4. Quick Check:

    Always check isset() before reading cookie [OK]
Hint: Always check isset() before using $_COOKIE values [OK]
Common Mistakes:
  • Ignoring isset() check causing warnings
  • Thinking cookie names are case sensitive in $_COOKIE
  • Believing cookies cannot be read in PHP
5. You want to set a cookie named theme with value dark that expires in 7 days. Which code correctly sets this cookie?
hard
A. setcookie('theme', 'dark', time() + 60);
B. setcookie('theme', 'dark', time() + 7 * 24 * 60 * 60);
C. setcookie('theme', 'dark', 7);
D. setcookie('theme', 'dark', time() - 7 * 24 * 60 * 60);

Solution

  1. Step 1: Calculate expiration time for 7 days

    7 days in seconds = 7 * 24 hours * 60 minutes * 60 seconds = 604800 seconds.
  2. Step 2: Use time() plus 7 days for expiration

    The expiration parameter in setcookie() should be current time plus 604800 seconds.
  3. Step 3: Check options for correct expiration

    setcookie('theme', 'dark', time() + 7 * 24 * 60 * 60); correctly uses time() + 7 * 24 * 60 * 60. setcookie('theme', 'dark', time() + 60); sets only 60 seconds, C uses 7 (invalid), D sets a past time (cookie expires immediately).
  4. Final Answer:

    setcookie('theme', 'dark', time() + 7 * 24 * 60 * 60); -> Option B
  5. Quick Check:

    Expire = now + 7 days in seconds [OK]
Hint: Expire time = time() + days * 86400 seconds [OK]
Common Mistakes:
  • Using small numbers like 7 or 60 instead of seconds
  • Setting expiration in the past
  • Forgetting to add time() to expiration
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard