What if your users' login info stayed forever, even after they left a public computer?
Why Cookie expiration and security in PHP? - Purpose & Use Cases
Imagine you run a website where users log in. You want to keep them logged in for a while, but also make sure their login info doesn't stay forever. Without setting cookie expiration and security properly, you might have to manually track sessions and guess when to log users out.
Manually managing user sessions without cookie expiration is slow and risky. Users might stay logged in too long, risking their accounts if they use public computers. Also, without security flags, cookies can be stolen by attackers, leading to data breaches.
Using cookie expiration and security settings lets you control exactly how long cookies last and protect them from theft. This means users stay logged in just the right amount of time, and their data stays safe without extra manual work.
$cookie = $_COOKIE['user']; // no expiration or security // manually check session timeout in code
setcookie('user', 'value', time() + 3600, '/', '', true, true); // expires in 1 hour, secure and httponly flags
It enables safe, automatic control over user sessions and protects sensitive data from attackers.
A shopping website keeps users logged in for 30 minutes after inactivity and uses secure cookies to prevent hackers from stealing login info.
Manual session tracking is slow and risky.
Cookie expiration controls how long login info stays valid.
Security flags protect cookies from theft and misuse.
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
