What is session management?

In short
Session management is how a web application remembers a user across many separate requests. The app gives each visitor a session ID, a random token that is sent back and forth with every request, and it keeps the user's details on the server. Done well, it keeps the user signed in safely.
Why web apps need sessions
HTTP, the protocol browsers use to talk to websites, is stateless. Each request and response pair stands on its own, and the server does not automatically remember who sent the previous one. A session fills that gap. It is a series of requests that belong to the same user, and the application keeps track of them as one thing.
Sessions are useful even before a person logs in. A site can remember a visitor's language preference from the first page view. After login, the session lets the app recognize the user on every later request, check what that user is allowed to do, and show only their own private data.
How it works
When a session starts, the application creates a session ID, sometimes called a token. This is a name and value pair that the browser stores and sends with every request. The server uses the ID to look up the session record, which holds details such as the user's role, access rights, and last login time.
The ID itself should mean nothing on its own. It is only a pointer. All the meaningful data lives on the server, in session objects or in a session store. If that stored data includes sensitive values, such as payment details, the store should be encrypted and protected.
The session ID is a strong key. Once a user has signed in, possessing the ID is almost as good as being that user. This is why the ID must be hard to guess and hard to steal.
Choosing a strong session ID
The main rule is randomness. The official guidance says a session ID should carry at least 64 bits of entropy, which means it should be unpredictable enough that an attacker cannot simply guess one by trying many values. Entropy is a measure of randomness. Each extra bit doubles the number of possible values.
The length of the ID depends on how it is encoded. When written as hexadecimal, an ID needs at least 16 characters to reach 64 bits. Other encodings need a different length. If part of the ID is fixed or predictable, the real randomness drops, so the whole ID must be random or the total length must grow.
The random values should come from a cryptographically secure random number generator. A normal random function may produce patterns that an attacker can study. Most frameworks already do this for you, so the usual advice is to use the session ID your language or framework creates rather than inventing your own.
Cookies and other ways to carry the ID
The ID can travel in several ways: a cookie, a URL parameter, a hidden form field, or a custom header. Cookies are the most common choice because they support useful settings such as expiration times and usage limits. Putting the ID in the URL is risky. It can leak into browser history, server logs, bookmarks, and the Referer header sent to other sites.
Frameworks such as PHP, ASP.NET, and J2EE come with built-in session features. Their default cookie names, such as PHPSESSID or JSESSIONID, reveal which technology the site uses. Renaming the cookie to a generic name is a small but recommended step.
Common mistakes
A frequent mistake is putting user details, such as an email address or role, directly into the session ID. Anyone who decodes the value can then read it. Another mistake is reusing or never changing the ID after login, which opens the door to session fixation, where an attacker plants a known ID in a victim's browser.
Session hijacking is the attack that follows when an ID is exposed, guessed, or predicted. The attacker then acts as the victim inside the application. Targeted attacks aim at one specific user, while generic attacks aim at any user who can be taken over.
Where to go next
Session management works closely with authentication, which proves who the user is, and with access control, which decides what that user may do. Read the session guidance together with those topics, because a weakness in any one of the three can undo the others.
FAQ
What is the difference between a session and a cookie?
A session is the server-side record of a user's activity across requests. A cookie is one common way the browser carries the session ID back to the server with each request.
How long should a session ID be?
The guidance says a session ID needs at least 64 bits of entropy. In hexadecimal that means at least 16 characters, and other encodings need a different length. Randomness matters more than length alone.
Why should I not store user data inside the session ID?
Anything stored in the ID can be decoded by anyone who sees it. Keep the ID meaningless and store the user details on the server instead.
Can I make my own session ID generator?
The advice is to use the session ID your framework provides. If you must write your own, use a cryptographically secure random number generator and make sure every ID is unique.
Sources
This page is based on the official documentation below. Read it for full detail.
Session management — official documentation ↗