A session cookie is a temporary cookie deleted when the browser closes. Learn how session cookies work, examples, session vs persistent cookies, security and GDPR consent rules.
A session cookie is a small text file that a website stores in your browser's memory only for the length of your visit. It has no expiry date, so the browser deletes it when the browsing session ends, usually when you close the browser. Websites use session cookies to remember things between pages, such as that you are logged in or what is in your shopping cart.
Session cookies are also called transient cookies, temporary cookies, in-memory cookies or non-persistent cookies.
| Property | Session cookie |
|---|---|
| Lifetime | Until the browser session ends |
| Expiry date | None: no Expires or Max-Age attribute is set |
| Where it is stored | In the browser's memory, not written to disk as a long-term cookie |
| Typical content | A random session ID that points to data kept on the server |
| Typical purpose | Logins, shopping carts, multi-step forms, security tokens, load balancing |
| Party | Usually first-party, set by the website you are visiting |
| Consent under GDPR | Not needed if strictly necessary; needed for analytics or advertising |
HTTP is stateless: each time your browser requests a page, the server treats it as a new, unrelated request. Session cookies give the server a way to recognise the same visitor from one page to the next.
A server response that sets a session cookie looks like this:
Set-Cookie: sessionid=a3fWa9Kx72Lp; Path=/; Secure; HttpOnly; SameSite=Lax
A persistent cookie, by contrast, includes an expiry:
Set-Cookie: lang=en; Path=/; Max-Age=31536000; Secure; SameSite=Lax
In JavaScript, a cookie set without an expiry is also a session cookie:
document.cookie = "step=2; path=/; Secure; SameSite=Lax";
Common session cookie names include PHPSESSID (PHP), JSESSIONID (Java), ASP.NET_SessionId (ASP.NET), connect.sid (Node.js Express) and sessionid (Django).
| Session cookies | Persistent cookies | |
|---|---|---|
| Expiry | When the browser session ends | On a set date, or after a set number of seconds |
| Set with | No Expires or Max-Age | Expires or Max-Age attribute |
| Storage | Browser memory | Written to disk and kept after the browser closes |
| Survives closing the browser | No (unless session restore is on) | Yes |
| Typical use | Login state, cart, security, load balancing | "Remember me", saved preferences, analytics, advertising |
| Tracking across visits | No | Yes |
| Consent under GDPR | Often exempt as strictly necessary | Usually required, unless strictly necessary |
Many sites use both. For example, a login system might use a session cookie for the current visit and a persistent "remember me" cookie to log you back in next week.
Not always. If your browser is set to restore your previous session, such as "Continue where you left off" in Chrome or "Open previous windows and tabs" in Firefox, it may keep session cookies after a restart. Some mobile browsers rarely end a session at all because the app is not fully closed. That is why servers should also expire sessions themselves after a period of inactivity.
These terms are often confused:
Learn more in our guide to cookies and local storage.
Most session cookies are first-party: they are set by the domain in the address bar and used to make that site work. Third-party services embedded on a page, such as payment widgets, chat tools or video players, can also set session cookies on their own domains. Browsers increasingly restrict third-party cookies, so these are less reliable than first-party ones.
Session cookies do not contain viruses and cannot run code. They usually hold only a random ID. The risk is that anyone who steals a valid session ID can act as that user until the session ends. This is called session hijacking.
Protect session cookies with these attributes and practices:
It depends on what the cookie does, not how long it lasts. Under Article 5(3) of the ePrivacy Directive, storing or reading cookies needs prior consent unless the cookie is strictly necessary for a service the user has asked for. The GDPR sets the standard for that consent and applies to any personal data the cookie collects.
The Article 29 Working Party's Opinion 04/2012 on the cookie consent exemption lists typical cookies that do not need consent. Many of them are session cookies:
A session cookie still needs consent if it is used for analytics, advertising, social media tracking or anything that is not strictly necessary. A short lifetime does not make a tracking cookie exempt.
Even when consent is not required, you must still tell users about the cookies you use, usually in a cookie policy or cookie declaration. The UK PECR, the California CCPA/CPRA and other privacy laws have similar rules about disclosing cookies.
To check all cookies on your site at once, including which ones are session cookies, run a free scan with the UniConsent Cookie Checker.
UniConsent is a Google-certified and IAB Europe-registered Consent Management Platform (CMP). It helps you manage session cookies alongside every other cookie on your site:
A session cookie lasts until the browser session ends, usually when you close the browser. If the browser restores sessions on restart, it can last longer. The server-side session it points to often times out sooner, commonly after 15 to 30 minutes of inactivity.
In the browser's memory. They are not saved as long-term cookies on disk, although browsers with session restore may save them temporarily so they can reopen your tabs.
A session cookie on its own cannot track you across visits because it is deleted at the end of the session. Within a single visit it can link your page views together, which is why session cookies used for analytics or advertising still need consent under the GDPR.
They can be. The GDPR treats online identifiers, such as a session ID linked to a logged-in user, as personal data when they can be used to identify a person.
It is not recommended. Blocking session cookies will break logins, shopping carts and checkout on most websites. To limit tracking, block third-party cookies and refuse non-essential cookies in consent banners instead.
A session ID is the random value that identifies your session on the server. A session cookie is the most common way to store that ID in the browser and send it back with each request.
Get started to make your website and application compliant for EU GDPR, US CPRA, CA PIPEDA etc
Sign up