MeshWorld India Logo MeshWorld.
PHP PHP cookies vs sessions PHP sessions tutorial PHP cookie tutorial PHP user management Web development cookies sessions 3 min read

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

Vishnu
By Vishnu
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.