Cookies in PHP (Remember Me Feature)
Learn what cookies store, when to use them, and how to set them safely.
Olivia Sanchez
October 27, 2025
4.3k168
Cookies are small pieces of data stored in the browser.
Cookies vs Sessions (clear difference)
- Session: stored on server, browser has only the session id
- Cookie: stored on browser, sent with every request to your domain
Setting a cookie
setcookie("theme", "dark", time() + 60 * 60 * 24 * 30, "/");
That stores theme=dark for 30 days.
Reading a cookie
if (isset($_COOKIE["theme"])) {
echo "Theme: " . $_COOKIE["theme"];
}
Deleting a cookie
setcookie("theme", "", time() - 3600, "/");
Security note (very important)
Never store sensitive info directly in cookies (like password).
If you do “remember me”, store a random token and validate it on server.
Next: Error handling and debugging in PHP without guessing.
#PHP#Cookies#Beginner