A cookie is a small piece of data stored on the user's computer by the web browser. It helps websites remember information about the user, like login status or preferences.
Click to reveal answer
beginner
How do you set a cookie in PHP?
You use the setcookie() function. For example: setcookie('user', 'Alice', time() + 3600); sets a cookie named 'user' with value 'Alice' that expires in 1 hour.
Click to reveal answer
beginner
How do you read a cookie value in PHP?
You read cookies from the $_COOKIE superglobal array. For example: $user = $_COOKIE['user'] ?? 'Guest'; gets the 'user' cookie or 'Guest' if it doesn't exist.
Click to reveal answer
intermediate
What must you remember about when to call setcookie() in PHP?
You must call setcookie() before any output is sent to the browser, because it sends HTTP headers. If you print or echo anything first, setting cookies will fail.
Click to reveal answer
intermediate
How can you delete a cookie in PHP?
To delete a cookie, set it with an expiration time in the past. For example: setcookie('user', '', time() - 3600); tells the browser to remove the 'user' cookie.
Click to reveal answer
Which PHP function is used to set a cookie?
Acookie_set()
Bsetcookie()
Cset_cookie()
Dcookie()
✗ Incorrect
The correct function to set a cookie in PHP is setcookie().
Where are cookies stored?
AOn the user's computer
BOn the web server
CIn the PHP script
DIn the database
✗ Incorrect
Cookies are stored on the user's computer by the web browser.
How do you access a cookie named 'session' in PHP?
A$_SESSION['session']
Bgetcookie('session')
C$_COOKIE['session']
Dcookie('session')
✗ Incorrect
Cookies are accessed using the $_COOKIE array, like $_COOKIE['session'].
What happens if you call setcookie() after outputting HTML?
AThe cookie will be deleted
BCookie will be set normally
CPHP will automatically buffer output and set cookie
DCookie will not be set because headers are already sent
✗ Incorrect
Cookies require HTTP headers, so calling setcookie() after output causes failure.
How do you delete a cookie named 'user'?
Asetcookie('user', '', time() - 3600);
Bunset($_COOKIE['user']);
Csetcookie('user', null);
Ddelete_cookie('user');
✗ Incorrect
To delete a cookie, set it with an expiration time in the past using setcookie().
Explain how to set and read a cookie in PHP with an example.
Think about how you tell the browser to save data and then how you get it back.
You got /3 concepts.
What are the important rules to remember when working with cookies in PHP?
Consider timing and how cookies expire or get removed.
You got /3 concepts.
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?