Cookies store small data on a user's browser. Setting expiration controls how long the cookie lasts. Security settings keep cookies safe from hackers.
Cookie expiration and security in PHP
setcookie(name, value, expire, path, domain, secure, httponly);
expire is a timestamp (seconds since 1970) when the cookie expires.
secure means cookie is sent only over HTTPS.
httponly means cookie cannot be accessed by JavaScript.
setcookie("user", "Alice", time() + 3600);
setcookie("session", "abc123", 0, "/", "example.com", true, true);
setcookie("cart", "5", time() + 86400, "/shop");
This program sets a cookie named 'username' that lasts 2 hours, is sent only over HTTPS, and is not accessible by JavaScript. It then checks if the cookie exists and greets the user accordingly.
<?php // Set a cookie that expires in 2 hours, secure and httponly setcookie("username", "Bob", time() + 7200, "/", "", true, true); // Check if cookie is set if (isset($_COOKIE["username"])) { echo "Welcome back, " . $_COOKIE["username"] . "!"; } else { echo "Hello, new visitor!"; } ?>
Cookies must be set before any HTML output in PHP.
Use time() + seconds to set expiration in the future.
Setting secure to true requires HTTPS connection.
Cookie expiration controls how long the cookie stays on the browser.
Security flags like secure and httponly protect cookies from theft and misuse.
Always set cookies before sending any output in PHP.
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
