Cookie expiration and security in PHP - Time & Space Complexity
When setting cookies in PHP, it's important to understand how the time it takes to set cookies grows as you add more cookies or set complex options.
We want to see how the code's work changes when handling cookie expiration and security settings.
Analyze the time complexity of the following code snippet.
// Set multiple cookies with expiration and security options
$cookies = [
['name' => 'user', 'value' => 'Alice'],
['name' => 'session', 'value' => 'abc123'],
['name' => 'prefs', 'value' => 'dark_mode']
];
foreach ($cookies as $cookie) {
setcookie($cookie['name'], $cookie['value'], time() + 3600, '/', '', true, true);
}
This code sets several cookies, each with an expiration time and security flags.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The foreach loop that sets each cookie.
- How many times: Once for each cookie in the array.
Each cookie requires one call to setcookie, so the work grows directly with the number of cookies.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 calls to setcookie |
| 10 | 10 calls to setcookie |
| 100 | 100 calls to setcookie |
Pattern observation: The number of operations grows evenly as the number of cookies increases.
Time Complexity: O(n)
This means the time to set cookies grows in a straight line with the number of cookies you set.
[X] Wrong: "Setting cookie expiration or security options makes the code slower in a complex way."
[OK] Correct: These options are just parameters in each call and do not add extra loops or repeated work; the main cost depends on how many cookies you set.
Understanding how your code scales when managing cookies helps you write efficient web applications and shows you can think about performance in real situations.
"What if we nested the cookie-setting inside another loop that runs multiple times? How would the time complexity change?"
Practice
expires parameter in setcookie() control?Solution
Step 1: Understand the purpose of the expires parameter
Theexpiresparameter sets the time when the cookie will be deleted by the browser.Step 2: Relate expires to cookie lifetime
Settingexpirescontrols how long the cookie remains stored on the user's device.Final Answer:
How long the cookie stays on the user's browser -> Option AQuick Check:
Cookie expiration = lifetime on browser [OK]
- Confusing expires with cookie name or value
- Thinking expires controls cookie content
- Mixing expires with domain or path
Solution
Step 1: Recall setcookie() parameters order
The parameters are: name, value, expire, path, domain, secure, httponly.Step 2: Identify correct secure and httponly flags
Settingsecureto true means cookie sent only over HTTPS, andhttponlytrue means inaccessible to JavaScript.Final Answer:
setcookie('user', 'John', time() + 3600, '/', '', true, true); -> Option CQuick Check:
Secure and HttpOnly flags = true, true [OK]
- Mixing order of parameters
- Setting secure or httponly to false by mistake
- Omitting parameters causing errors
setcookie('test', '123', time() - 3600);
if(isset($_COOKIE['test'])) { echo 'Cookie set'; } else { echo 'Cookie not set'; }Solution
Step 1: Understand the expiration time used
The cookie is set with an expiration time in the past (time() - 3600), which deletes the cookie immediately.Step 2: Check if cookie exists after deletion
Since the cookie is expired,$_COOKIE['test']will not be set, so the else branch runs.Final Answer:
Cookie not set -> Option BQuick Check:
Expired cookie = not set [OK]
- Assuming cookie is set immediately after setcookie
- Ignoring expiration time meaning
- Confusing server and client cookie states
setcookie('session', 'abc123', time() + 3600, '/', '', 'true', true);Solution
Step 1: Check parameter types in setcookie()
Thesecureparameter must be a boolean (true/false), not a string.Step 2: Identify the incorrect usage
Here, 'true' is a string, which PHP treats as true but is incorrect usage and can cause unexpected behavior.Final Answer:
The secure flag is a string instead of boolean -> Option AQuick Check:
Secure flag must be boolean, not string [OK]
- Using 'true' or 'false' as strings
- Forgetting to set secure flag for HTTPS
- Omitting required parameters
auth that lasts exactly 2 hours, is only sent over HTTPS, and cannot be accessed by JavaScript. Which of the following is the best way to do this?Solution
Step 1: Calculate correct expiration time
2 hours = 7200 seconds, so expiration should betime() + 7200.Step 2: Set secure and httponly flags correctly
Secure flag true ensures HTTPS only; httponly true prevents JavaScript access.Step 3: Verify all parameters
Path is '/', domain is default '', flags are true, true matching requirements.Final Answer:
setcookie('auth', 'token', time() + 7200, '/', '', true, true); -> Option DQuick Check:
2 hours + secure + httponly = setcookie('auth', 'token', time() + 7200, '/', '', true, true); [OK]
- Using wrong expiration time
- Not setting secure flag for HTTPS
- Leaving httponly false allowing JS access
Ready to prove it?
Pass the test and this pattern moves unknown
