DEV Community

Sreekar Reddy
Sreekar Reddy

Posted on • Originally published at sreekarreddy.com

๐ŸŽซ Sessions Explained Like You're 5

A hotel room key card

Day 45 of 149

๐Ÿ‘‰ Full deep-dive with code examples


The Hotel Key

At a hotel:

  1. You check in at the front desk
  2. They give you a key card
  3. Key card opens YOUR room, not others
  4. When you check out, key stops working

Sessions work the same way!


How It Works

  1. You log in to a website
  2. Server creates a "session" for you
  3. Gives your browser a session ID (like a key card)
  4. Session ID unlocks YOUR data on the server
Login โ†’ Server creates Session #12345
     โ†’ Browser stores ID: "12345"

Every request: "I'm session 12345"
Server: "Ah yes, that's Alex! Show Alex's dashboard."
Enter fullscreen mode Exit fullscreen mode

Sessions vs Cookies

Cookie: Data stored in YOUR browser
Session: Data stored on the SERVER, you just have the ID

Sessions can reduce exposure because sensitive data typically stays on the server (the browser usually only keeps an ID).


When Sessions End

  • You log out (check out)
  • You close the browser
  • Too much time passes (timeout)

In One Sentence

Sessions let servers remember who you are by giving your browser an ID that links to your data on the server.


๐Ÿ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

Top comments (0)