What if your favorite website could remember you perfectly without asking again and again?
How cookies work in PHP - Why You Should Know This
Imagine you visit an online store and add items to your cart. Without cookies, every time you click a new page, the website forgets what you added. You have to start over, adding items again and again.
Manually tracking your choices on every page means the website must send all your data back and forth each time. This is slow, confusing, and easy to lose information. It feels like repeating the same steps endlessly.
Cookies let the website save small pieces of information on your computer. This way, the site remembers your cart, login, or preferences as you browse, making your experience smooth and personal without asking you to repeat actions.
$cart = []; // No way to remember cart between pages // User must add items every time
setcookie('cart', json_encode($cart), time() + 3600); // Cart saved in cookie, remembered across pages
Cookies enable websites to remember you and your choices, creating a seamless and personalized browsing experience.
When you log into a social media site, cookies keep you logged in as you click around, so you don't have to enter your password on every page.
Without cookies, websites forget your actions between pages.
Cookies store small data on your device to remember your choices.
This makes browsing faster, easier, and more personal.
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
