A cookie is a small value stored in the user's browser: setcookie("theme", "dark", time() + 86400 * 30);. Read it with $_COOKIE["theme"].
Sessions
A session stores data on the server, linked to the user by a session id cookie. It is the standard way to keep users logged in.
session_start(); at the very top of every page that uses sessions.
Save: $_SESSION["user"] = "Ali";
Read: $_SESSION["user"]
Log out: session_destroy();
Call session_regenerate_id(true) after login to prevent session-fixation attacks.
💡 Key Takeaways: PHP Cookies & Sessions
Mastering PHP Cookies & Sessions helps you write clean and reliable PHP code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.