Cookies help websites remember you. They store small bits of information on your computer so the site can recognize you later.
How cookies work in PHP
<?php setcookie(name, value, expires, path, domain, secure, httponly); ?>
name is the cookie's name.
value is the data stored in the cookie.
expires is the time the cookie expires (as a Unix timestamp).
Only name and value are required; others are optional.
<?php setcookie("user", "Alice"); ?>
<?php setcookie("theme", "dark", time() + 3600); ?>
<?php setcookie("session", "12345", time() + 86400, "/", "example.com", true, true); ?>
This program sets a cookie called 'visitor' with the value 'John' that lasts for one hour. Then it checks if the cookie exists and greets the visitor by name if it does. If not, it welcomes a new visitor.
<?php // Set a cookie named 'visitor' with value 'John' that expires in 1 hour setcookie("visitor", "John", time() + 3600); // Check if the cookie is set and print a message if (isset($_COOKIE["visitor"])) { echo "Welcome back, " . $_COOKIE["visitor"] . "!"; } else { echo "Hello, new visitor!"; } ?>
Cookies must be set before any HTML or output is sent to the browser.
Cookies are stored on the user's computer and can be deleted by the user anytime.
Cookies can only store strings, so complex data must be converted (e.g., using JSON).
Cookies store small data on the user's computer to remember information.
Use setcookie() in PHP to create cookies.
Cookies help improve user experience by saving preferences and login info.
Practice
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 DQuick Check:
Cookies store data = B [OK]
- Confusing cookies with server-side code execution
- Thinking cookies style web pages
- Assuming cookies manage database connections
user with value John in PHP?Solution
Step 1: Recall the PHP function to set cookies
The correct PHP function issetcookie()with parameters as strings.Step 2: Check syntax correctness
setcookie('user', 'John'); uses correct function name and quotes around parameters, making it valid syntax.Final Answer:
setcookie('user', 'John'); -> Option AQuick Check:
Correct function and quotes = A [OK]
- Using wrong function names like cookie_set or set_cookie
- Forgetting quotes around cookie name or value
- Passing variables without quotes incorrectly
setcookie('color', 'blue');
if(isset($_COOKIE['color'])) {
echo $_COOKIE['color'];
} else {
echo 'No cookie set';
}Solution
Step 1: Understand when cookies are available
Cookies set bysetcookie()are available on the next page load, not immediately in the same script.Step 2: Check the condition in the code
Since the cookie is not yet set in$_COOKIEduring this script run,isset($_COOKIE['color'])is false, so it prints 'No cookie set'.Final Answer:
No cookie set -> Option BQuick Check:
Cookie available next request = A [OK]
- Expecting cookie immediately after setcookie() in same script
- Assuming $_COOKIE is updated instantly
- Confusing cookie setting with session variables
setcookie('theme', 'dark', time() + 3600)
echo 'Cookie set!';Solution
Step 1: Check PHP syntax rules
Each PHP statement must end with a semicolon (;). Thesetcookie()call is missing this.Step 2: Verify function name and parameters
The function name and parameters are correct, and expiration time is valid.Final Answer:
Missing semicolon after setcookie() call -> Option AQuick Check:
Semicolon missing = C [OK]
- Forgetting semicolon at end of function calls
- Thinking expiration time is invalid
- Confusing function names
setcookie() calls correctly achieves this?Solution
Step 1: Calculate expiration time for 7 days
7 days = 7 * 24 * 3600 = 604800 seconds, sotime() + 604800is correct.Step 2: Set secure and httponly flags
To make cookie HTTPS-only, set thesecureflag totrue. To improve security,httponlyshould also betrue.Step 3: Check options
setcookie('session', 'active', time() + 604800, '/', '', true, true); sets secure and httponly to true, matching requirements. Others either disable secure or httponly or omit parameters.Final Answer:
setcookie('session', 'active', time() + 604800, '/', '', true, true); -> Option CQuick Check:
Secure and httponly true for HTTPS cookie = D [OK]
- Not setting secure flag to true for HTTPS-only cookies
- Omitting httponly flag for security
- Incorrect expiration time calculation
Ready to prove it?
Pass the test and this pattern moves unknown
