🍪 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.
Related Articles
Deepen your understanding with these curated continuations.
Unsigned Columns in Laravel Migrations
Master Laravel 12 migrations with unsigned columns. Learn foreignId(), unsignedBigInteger(), and best practices for foreign key constraints in modern PHP applications.
PHP Input Validation & Sanitization: Railway Ticket Booking Guide
Learn PHP input validation and sanitization with a fun railway ticket booking analogy. Protect your site from SQL injection and XSS with practical examples.
PHP self:: vs static:: Explained: A Simple Guide
Learn the key differences between self:: and static:: in PHP. Understand late static binding, class inheritance, and when to use each for cleaner OOP code.