How cookies work in PHP - Performance & Efficiency
We want to understand how the time it takes to handle cookies changes as we work with more cookies.
How does the program's work grow when reading or writing cookies?
Analyze the time complexity of the following code snippet.
// Set multiple cookies
foreach ($cookies as $name => $value) {
setcookie($name, $value, time() + 3600);
}
// Read cookies
foreach ($_COOKIE as $name => $value) {
echo "$name: $value\n";
}
This code sets several cookies and then reads all cookies sent by the browser.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of cookies to set and then looping through all cookies to read.
- How many times: Each loop runs once for every cookie in the list.
As the number of cookies grows, the work grows in a straight line with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (10 to set + 10 to read) |
| 100 | About 200 operations (100 to set + 100 to read) |
| 1000 | About 2000 operations (1000 to set + 1000 to read) |
Pattern observation: The total work grows directly with the number of cookies.
Time Complexity: O(n)
This means the time to handle cookies grows in a straight line as the number of cookies increases.
[X] Wrong: "Handling cookies takes the same time no matter how many cookies there are."
[OK] Correct: Each cookie adds a little more work, so more cookies mean more time spent.
Understanding how cookie handling scales helps you think about performance in web apps, a useful skill for many programming tasks.
"What if we only read cookies once but set them multiple times? How would the time complexity change?"
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
