PHP Cookies ๐ช vs Sessions ๐: Complete Guide with Code Examples
๐ช Cookies: Your Personalized Theme Park Wristband ๐๏ธ
Imagine a website as a big, exciting theme park. A cookie is like a special wristband ๐ท๏ธ the park gives you at the entrance. This wristband can hold small bits of info like:
- Your name ๐ง
- Your favorite theme (light ๐ or dark ๐)
- Your preferred language ๐ (like
enfor English,hifor Hindi)
โณ Cookies are stored on your computer or browser and can persist for a set time (days, months). Even if you leave and come back tomorrow, the wristband stays with you!
How Cookies Work in PHP
- To create a cookie, use the
setcookie()function, which sets the wristband on your visitorโs browser:
<?php
setcookie("theme", "dark", time() + (86400 * 30), "/"); // lasts 30 days
?>
- To check and read a cookieโs value:
<?php
if (isset($_COOKIE["theme"])) {
echo "The user prefers the " . $_COOKIE["theme"] . " theme!";
} else {
echo "User preference unknown.";
}
?>
๐น Key Cookie Facts:
- Stored on the client side (userโs machine) ๐ป
- Size limit of about 4KB
- Not secure for sensitive data (users can view/modify)
๐ Sessions: The Theme Parkโs Safe Locker ๐๏ธ
A session is like a secure locker inside the theme park ๐ข where you safely store your belongings. The website stores session data on the server, and you get a special key (session ID) ๐ to access it.
Sessions store larger info like:
- Shopping cart items ๐
- User login details ๐
- Arrays or complex data
โณ Sessions last only during your visit โ when you close your browser, the locker empties.
How Sessions Work in PHP
- Start the session to get your locker key:
<?php
session_start(); // get session key
$_SESSION["shopping_cart"] = ["t-shirt", "hat", "sunglasses"];
echo "Items saved in your cart!";
?>
- To retrieve session data:
<?php
session_start();
print_r($_SESSION["shopping_cart"]);
?>
๐น Key Session Facts:
- Stored server side (more secure) ๐ฅ๏ธ
- Can handle much more data than cookies
- Lifespan limited to browser session (temporary)
๐ฅ Cookies vs Sessions โ Quick Comparison
| Feature | Cookies ๐ช | Sessions ๐ |
|---|---|---|
| Storage location | On client browser ๐ป | On the server ๐ฅ๏ธ |
| Size limit | Small (~4KB) | Large (server limit) |
| Security | Can be modified by user โ ๏ธ | Safer, stored server-side |
| Duration | Persistent (days, months) โณ | Until browser closes โฑ๏ธ |
| Typical use | Preferences, language, themes | Login, carts, sensitive data |
| Analogy | Wristband ๐๏ธ | Safe locker ๐๏ธ |
๐ฆธ Fun Exercise: Build Your Superhero Website
Challenge 1: Cookie ๐ช
Remember the superheroโs language preference "gu" (Gujarati):
<?php
setcookie("language", "gu", time() + (86400 * 30), "/");
?>
Challenge 2: Session ๐
Store superhero gadgets in session:
<?php
session_start();
$_SESSION["gadgets"] = ["utility belt", "Batarang"];
?>
Summary ๐ฏ
- Cookies ๐ช โ Client-side, lightweight, persistent wristbands that remember your preferences.
- Sessions ๐ โ Server-side, secure lockers storing bigger and sensitive info temporarily.