What are sessions and cookies?
Websites forget you between every click unless something reminds them. Sessions and cookies are that reminder: a coat check ticket for the web, with a few flags that decide how safe it is.

Key takeaways
- HTTP has no memory; a session is the server-side or token-based record that you are signed in, and a cookie is one way to carry it.
- Cookie flags matter: HttpOnly blocks page scripts, Secure requires HTTPS, SameSite limits cross-site sending.
- Session length is a product decision: a banking app might end sessions after 15 idle minutes, a game might keep players signed in for weeks.
A session is an app's memory that you are signed in across many separate requests, and a cookie is a small piece of text a website asks your browser to store and send back on every later visit. Cookies are the classic way to carry a session, though not the only one. Without either, a website would forget who you are the moment you clicked to the next page.
An everyday example: the coat check
You hand over your coat and get ticket number 57. You do not tell the attendant your name each time you pass; you show the ticket and they know which coat is yours. On the web, HTTP is the forgetful attendant: each request arrives with no memory of the last. After you sign in, the server makes a session (the coat on the hook, with your user ID and when it expires) and gives the browser a ticket in a cookie, like `session_id=8f3a...`. The browser shows that ticket automatically with every request to the same site.
Two ways apps keep sessions
- Server sessions: the server keeps a table of sessions and the cookie holds only a random ID. Signing someone out means deleting their row, effective immediately.
- Token sessions: after sign-in, the client holds a signed token such as a JWT plus a refresh token, and sends it with each request. The server verifies the signature instead of looking up a row. Many single-page apps and mobile apps work this way, storing tokens in browser storage or the phone's secure storage rather than in a cookie.
The cookie settings that matter
- HttpOnly: page scripts cannot read the cookie, so injected code cannot steal the session.
- Secure: the cookie is only sent over HTTPS.
- SameSite: `Lax` (a sensible default) or `Strict` stops the browser sending the cookie on most requests started by other sites, which blocks a class of attacks called cross-site request forgery. `None` allows it and requires Secure.
- Expires or Max-Age: without one, the cookie disappears when the browser closes.
How long a session should last is a product choice. Reasonable starting points: 15 to 30 idle minutes for money or health data, around 30 days with refresh for a social or productivity app, longer for a casual game where being signed out is just annoying.
Why it matters when building with Startbase
When Startbase Cloud handles sign-in, the auth library manages the session for you: tokens, renewal and sign-out. Your part is choosing behavior. The Startbase security practices checklist is worth following: use built-in session management for token handling, redirect on expired sessions but always re-validate on the server, and use the frontend's signed-in state only for what the screen shows. If you add analytics or marketing tools that set their own cookies, your privacy policy and, for visitors in the EU, a consent banner come into play.
Keep users signed in on this device for 30 days unless they sign out. If their session expires while they are filling in a form, save the draft, send them to sign in, and bring them back to the same form with the draft restored. Add a "Sign out of all devices" button in settings.
Common confusions
- Not every cookie is a tracker. A session cookie that keeps you signed in is strictly necessary; third-party advertising cookies are the ones consent rules target.
- Clearing cookies signs you out, which is why "try clearing cookies" fixes stuck sign-in states.
- Browser storage is not a cookie. localStorage is never sent to the server automatically and any script on the page can read it.
Frequently asked questions
Why do I get signed out when I switch browsers?
Cookies and browser storage belong to one browser on one device. A new browser has no ticket to show, so you sign in again and get a new session.
Do mobile apps use cookies?
Usually not for sign-in. Native and React Native apps typically keep tokens in the phone's secure storage and send them in a request header.

