WhatIsWikiWhatIsWiki
  • Blog
  • Topics
WhatIsWikiWhatIsWiki
  • Blog
  • Topics

Get new explainers in your inbox

Short, practical updates. No spam. Unsubscribe anytime.

WhatIsWikiWhatIsWiki© 2026 WhatIsWiki
  • Blog
  • Topics
  • Authors
  • About
  • Contact
  • Editorial
  • Privacy
  • Sitemap
  • RSS
  1. Home
  2. /Web Development
  3. /What Is a Session in Web Development? Sessions vs. Cookies Explained (With Code Examples)

Web Development

What Is a Session in Web Development? Sessions vs. Cookies Explained (With Code Examples)

HTTP is stateless by design. Sessions (usually paired with a cookie holding a session ID) are how servers remember who's making the next request.

In short

A session lets a server remember a user across requests — because HTTP itself can't. Learn how sessions work, how they differ from cookies, and see real PHP/Express examples.

By Shubh Singh

Published July 29, 2026

Updated August 2, 2026

6 min read

14 reads

Beginner

What Is a Session? — Web Development explainer cover
What Is a Session? — Web Development explainer cover
  • web-development
  • nodejs
  • http
  • security
  • sessions
  • cookies
  • authentication
  • Php

Cite this page: https://www.whatiswiki.com/what-is-session

Introduction

Quick answer: A session is a way for a web server to remember information about a specific user across multiple requests — like keeping them logged in, or remembering what's in their shopping cart — even though the underlying HTTP protocol itself has no memory at all. HTTP is stateless by design: every request is treated as brand new, with zero awareness of any previous request. Sessions (typically paired with a cookie holding a session ID) are the mechanism developers use to work around that limitation.

This page replaces an earlier version that incorrectly suggested sessions were introduced as part of HTTP/1.0. HTTP has no native session concept — sessions exist specifically as a workaround for that limitation. Last updated August 2, 2026.

Table of contents13 sections
  1. 1.Introduction
  2. 2.Why sessions exist in the first place (the part most explainers skip)
  3. 3.How a session actually works, step by step
  4. 4.Sessions vs. cookies: the actual difference
  5. 5.What sessions are used for in practice
  6. 6.Session-based authentication, explained
  7. 7.Session hijacking, briefly
  8. 8.Sessions in code: quick examples
  9. 9.Important disambiguation: the OSI "session layer" is a different thing
  10. 10.Common misconceptions
  11. 11.Key takeaways
  12. 12.Frequently asked questions
  13. 13.Conclusion

Why sessions exist in the first place (the part most explainers skip)

This is the detail that actually makes sessions make sense, and it's worth getting right: HTTP was never designed to remember anything. When HTTP/1.0 was published in 1996, each request-response pair was treated as a fully independent transaction — the server had no built-in way to know that a second request came from the same browser as a first one.

That's a real problem for anything resembling a modern website. Without some workaround, a shopping site couldn't remember what you added to your cart between page loads, and a login would only last for a single page view. Sessions and cookies were invented specifically to solve this — cookies were introduced by Netscape in 1994, giving browsers a small piece of storage the server could ask for on every subsequent request. Sessions build on top of that mechanism to give the server actual memory of who's making the request.

How a session actually works, step by step

  1. A user visits a website for the first time.
  2. The server creates a new session and generates a unique session ID.
  3. That session ID is sent to the browser, usually stored in a cookie.
  4. On every subsequent request, the browser automatically sends that cookie back to the server.
  5. The server uses the session ID to look up the corresponding session data — which is stored server-side, not in the cookie itself.
  6. The server can then respond with content personalized to that specific user: their logged-in state, their cart contents, their preferences.

The key detail people most often get wrong: the cookie itself usually only holds the session ID — a reference — not the actual session data. The real data (login status, cart contents, etc.) lives on the server. This is exactly why sessions are considered more secure than storing sensitive data directly in a cookie.

Sessions vs. cookies: the actual difference

This is the single most common point of confusion, and it's worth being precise about:

CookiesSessions
Where storedClient-side (in the browser)Server-side
What they typically holdA session ID, or small pieces of non-sensitive dataThe actual user data (login state, cart, preferences)
Size limitsSmall (a few KB, and a per-domain cookie count limit)Effectively unlimited, since it lives on the server
SecurityVisible to the client; shouldn't hold sensitive data directlyMore secure, since sensitive data never leaves the server
LifespanCan be set to persist for days, months, or yearsTypically expires after a period of inactivity, or when the browser closes

In short: cookies are the delivery mechanism (usually just carrying an ID); sessions are where the actual remembered information lives. They work together, but they are not the same thing.

What sessions are used for in practice

  • Authentication — keeping a user logged in as they navigate between pages, without re-entering credentials on every request.
  • Shopping carts — remembering what a user has added, even across multiple page visits, until checkout or expiration.
  • Personalization — remembering language preference, theme settings, or previously viewed content.
  • Multi-step processes — like a checkout flow or a multi-page form, where information needs to persist between steps.

Session-based authentication, explained

When people search "session based authentication explained," they're usually trying to understand how login systems actually keep you logged in. Here's the flow:

  1. You submit your username and password.
  2. The server verifies your credentials.
  3. If valid, the server creates a session and stores your authenticated user ID against that session, server-side.
  4. The server sends your browser a cookie containing the session ID.
  5. On every future request, your browser sends that cookie back, and the server checks whether that session ID corresponds to a valid, authenticated session.
  6. If it does, you're treated as logged in — without re-sending your password on every single page.

This is different from token-based authentication (like JWTs), where the proof of authentication is held entirely on the client rather than looked up server-side — a common point of comparison in modern web development discussions, and a reasonable next topic if you're comparing authentication approaches.

Session hijacking, briefly

Since a session ID is effectively "proof of who you are" once you're logged in, an attacker who steals a valid session ID (through methods like cross-site scripting, network sniffing on insecure connections, or session fixation) can potentially impersonate that user without needing their actual password. This is why secure implementations use HTTPS, mark session cookies with security flags like HttpOnly and Secure, and expire sessions after a period of inactivity.

Sessions in code: quick examples

PHP (sessions are built into the language directly):

<?php
session_start(); // starts or resumes a session
$_SESSION['username'] = 'alex'; // store data in the session
echo $_SESSION['username']; // retrieve it on a later request
?>

Express (Node.js), using the express-session package:

const session = require('express-session');
app.use(session({
  secret: 'your-secret-key',
  resave: false,
  saveUninitialized: true
}));

app.get('/', (req, res) => {
  req.session.views = (req.session.views || 0) + 1;
  res.send(`You've visited this page ${req.session.views} times`);
});

Both examples show the same underlying pattern: the framework handles the cookie and session-ID bookkeeping automatically, so you just read and write to a session object as if it were a simple, persistent piece of storage per user.

Important disambiguation: the OSI "session layer" is a different thing

If you've come across "session layer in OSI model" and are wondering how it relates to web development sessions — it's a genuinely different, unrelated concept, despite the shared name. The OSI model's Session Layer (Layer 5) is a networking-theory concept describing how connections between two devices are established, maintained, and terminated at a lower, protocol level. Web application sessions (what this article covers) are an application-level concept built by developers on top of HTTP, which itself sits at OSI Layer 7. They share terminology, not a technical relationship — don't conflate the two when studying either topic.

Common misconceptions

  • "Sessions and cookies are the same thing." They're not — cookies are client-side storage, usually just holding a reference (the session ID); sessions are server-side data.
  • "Sessions are only for login/authentication." They're used for a wide range of purposes, including shopping carts, personalization, and multi-step form flows.
  • "HTTP has built-in session support." It doesn't — HTTP is stateless by design, and sessions are entirely a workaround built by application developers and frameworks, not a native protocol feature.

Key takeaways

  • ✓HTTP is stateless by design — sessions exist as a workaround so servers can remember users across requests.
  • ✓A session ID (usually in a cookie) is a reference; the real session data lives server-side.
  • ✓Cookies and sessions work together but are not the same thing.
  • ✓Common uses: authentication, shopping carts, personalization, and multi-step flows.
  • ✓Protect session IDs with HTTPS, HttpOnly/Secure cookies, and inactivity expiry to reduce hijacking risk.
  • ✓The OSI Session Layer is a different concept from web application sessions.

Frequently asked questions

What is a session in simple terms?

It's a way for a server to remember who you are and what you've done across multiple visits or page loads, even though the underlying HTTP protocol treats every request as brand new.

What's the difference between a session and a cookie?

A cookie is stored in the browser and usually just holds a session ID. A session is the actual data (login status, cart contents, etc.) stored on the server, referenced by that ID.

Is a session the same as session-based authentication?

Session-based authentication is one specific use of sessions — using a session to track whether a user is logged in. Sessions themselves are used for many other purposes too.

What is a session ID?

A unique identifier generated by the server for each active session, used to look up that session's stored data on subsequent requests — typically sent to and from the browser via a cookie.

Is the OSI session layer related to web development sessions?

No — despite the shared name, they're unrelated concepts from different levels of how networking and web applications work.

How do I explain a session in PHP?

In PHP, calling session_start() begins or resumes a session, after which you can store and retrieve data using the $_SESSION superglobal array, with the actual data kept server-side.

Conclusion

Sessions exist because HTTP won't remember you between requests. Pair a short-lived session ID in a cookie with server-side data, and you get login state, carts, and multi-step flows — without putting sensitive details in the browser. Treat the session ID like a credential: send it only over HTTPS, lock cookies down, and expire idle sessions.

References

  • HTTP Cookie overview and history
  • Cookies, Sessions, and Persistence
  • HTTP cookies and session management

Was this article helpful?

No login required. One response per visitor.

How this article was made

We write for readers first. Drafts may use research tools and generative AI for outlining and drafting, then are structured, fact-checked against editorial notes and primary sources when available, and published only if they pass our quality checks. Thin or duplicated explainers are not published.

See our editorial policy for authorship, corrections, and update standards.

Related articles

  1. →

    Jul 29, 2026 · Web Development

    What Is Authorization?

    Authorization is the process of determining whether a user or entity has the necessary permissions to access a particular resource or perform a specific action.

  2. ↓

    Jul 29, 2026 · Web Development

    What Is Frontend Development?

    Frontend development is the process of creating the user interface and user experience for websites and applications using programming languages like HTML, CSS, and JavaScript.

  3. ↓

    Jul 29, 2026 · Web Development

    What Is OAuth?

    OAuth is a widely used authorization framework that enables secure access to resources without password sharing.

  4. ↓

    Jul 28, 2026 · Technology

    What Is Angular?

    Angular is a popular JavaScript framework for building complex web applications.

  5. ↓

    Jul 29, 2026 · Technology

    What Is HTTP?

    HTTP, or Hypertext Transfer Protocol, is the foundation of data communication on the internet.

Share

About the author

SS

Shubh Singh

Shubh covers technology, business, and practical “what is…?” explainers for WhatIsWiki, with a focus on clear definitions, dates, and primary sources. He builds the site’s publishing systems and writes so readers leave with a usable answer—not more jargon.

Category

Web Development