📖 Scenario: You are building a simple website that remembers a visitor's favorite color using cookies. When the visitor selects a color, the website saves it in a cookie. When the visitor returns, the website reads the cookie and shows the favorite color.
🎯 Goal: Create a PHP script that sets a cookie named favorite_color with a specific value, then reads and displays the cookie value if it exists.
📋 What You'll Learn
Create a cookie named favorite_color with the value blue
Set the cookie to expire in 1 hour
Check if the cookie favorite_color exists
Display the message Your favorite color is: blue if the cookie exists
Display No favorite color set if the cookie does not exist
💡 Why This Matters
🌍 Real World
Cookies are used on websites to remember user preferences, login status, and other information to improve user experience.
💼 Career
Web developers often need to set and read cookies to manage sessions, personalize content, and track user behavior.
Progress0 / 4 steps
1
Create a cookie named favorite_color
Write a line of PHP code to set a cookie named favorite_color with the value blue that expires in 1 hour using the setcookie function.
PHP
Hint
Use setcookie("favorite_color", "blue", time() + 3600); to set the cookie.
2
Check if the cookie favorite_color exists
Add an if statement to check if the cookie favorite_color exists in the $_COOKIE superglobal array.
PHP
Hint
Use isset($_COOKIE["favorite_color"]) to check if the cookie is set.
3
Display the favorite color if the cookie exists
Inside the if block, write a print statement to display Your favorite color is: followed by the cookie value $_COOKIE["favorite_color"]. Add an else block that prints No favorite color set.
PHP
Hint
Use print("Your favorite color is: " . $_COOKIE["favorite_color"]) inside the if and print("No favorite color set") inside the else.
4
Run the script and see the output
Run the complete PHP script and observe the output. It should print Your favorite color is: blue if the cookie is set, or No favorite color set if not.
PHP
Hint
Run the script in a browser or PHP server to see the cookie message.
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
Step 1: Understand what cookies do
Cookies save small data on the user's computer to remember information like preferences or login status.
Step 2: Identify the correct purpose
Among the options, only storing small data on the user's computer matches the purpose of cookies.
Final Answer:
To store small pieces of data on the user's computer -> Option A
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
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.
Step 2: Check the options for correct syntax
Only setcookie('user', 'John'); uses the correct function name and quotes around name and value.
Final Answer:
setcookie('user', 'John'); -> Option A
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
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.
Step 2: Output the cookie value
Because the cookie exists, the code echoes its value, which is 'blue'.
Final Answer:
blue -> Option D
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
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.
Step 2: Identify the missing check
The code should use isset($_COOKIE['session']) before echoing to avoid errors.
Final Answer:
Missing check if cookie exists before reading -> Option C
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?