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.