MeshWorld India LogoMeshWorld.

PHP Cookies 🍪 vs Sessions 🔐: Complete Guide with Code Examples

Listen to ArticleAI Speech
~3 min read narration
100%
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 en for English, hi for 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

FeatureCookies 🍪Sessions 🔐
Storage locationOn client browser 💻On the server 🖥️
Size limitSmall (~4KB)Large (server limit)
SecurityCan be modified by user ⚠️Safer, stored server-side
DurationPersistent (days, months) ⏳Until browser closes ⏱️
Typical usePreferences, language, themesLogin, carts, sensitive data
AnalogyWristband 🎟️Safe locker 🗄️

🦸 Fun Exercise: Build Your Superhero Website

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.

Reader Quality Feedback

Did this technical guide help solve your problem?

Suggest Errata ($0)
Vishnu
Primary Author

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Explore Author Archive
Compute Fuel & Open Testbed
100% Independent & Verified

Fuel High-Density, Zero-Fluff Engineering Deep-Dives

Every guide on MeshWorld is validated on physical Linux nodes and reproducible testbeds. If this article saved you hours of debugging or unblocked production, consider funding our next cluster run.

Weekly Dispatch

Join MeshWorld Dispatch

Get practical tutorials, system blueprints, and curated AI engineering notes straight to your inbox. No fluff, zero spam.

Zero spam. 1-click unsubscribe anytime.Prefer RSS?
Curated Continuations

Up Next in This Domain.

Browse Full Archive