Cookies in PHP (Remember Me Feature)
Learn what cookies store, when to use them, and how to set them safely.
Olivia Sanchez
December 21, 2025
0.0k0
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
```php setcookie("theme", "dark", time() + 60 * 60 * 24 * 30, "/"); ```
That stores `theme=dark` for 30 days.
Reading a cookie
```php if (isset($_COOKIE["theme"])) { echo "Theme: " . $_COOKIE["theme"]; } ```
Deleting a cookie
```php 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