← Glossary

What Is a Session Cookie?

A session cookie is a cookie with no expiration date, cleared when the browser session ends. Learn how it works, common uses, and its limits.

Updated:

Session cookies are defined as cookies that do not have the Expires or Max-Age attributes, meaning that they only hold until the time of the current session. The cookie is automatically deleted once the session ends, instead of being saved on the disk to be used later. The term session cookie only describes the lifecycle of the cookie, rather than its content or purpose, meaning that session cookies can contain anything from the login token to the identifier of the shopping cart. Each time the server sends the Set-Cookie header, it decides if a certain cookie is a session cookie or a permanent cookie, which means that the same site can have both types of cookies for different purposes.

How Session Cookies Work

A server creates a cookie by sending a Set-Cookie header in its response, and the browser returns that cookie on later requests to the same site. The HTTP State Management Mechanism standard that defines this exchange specifies that a cookie becomes a session cookie by omission: if the server leaves out both the Expires and Max-Age attributes, the resulting cookie expires with the session rather than on a schedule.

What Ends a Session

A session generally ends when the browser itself closes, at which point session cookies tied to that browser are cleared. The exact boundary is defined by the browser, not by the server that set the cookie, since the browser is what tracks whether it has been closed and reopened. This matters because a server cannot force a session cookie to expire at a specific moment the way it can with a persistent one, and it also means two different browsers visiting the same site can treat an outwardly identical session cookie very differently depending on how each one defines "closed."

Session Cookies vs. Other Cookie Types

A cookie's session status is one of several properties a cookie might have, along with whether it is Secure or HttpOnly, and whether it can only be used in first-party contexts. All of these aspects are different properties that have independent configurations and thus it is possible for cookies to be session cookies that are also set as Secure or HttpOnly even though they do not have any expiration date.

Key Characteristics

A cookie's session status is one of several properties a cookie might have, along with whether it is Secure or HttpOnly, and whether it can only be used in first-party contexts. All of these aspects are different properties that have independent configurations and thus it is possible for cookies to be session cookies that are also set as Secure or HttpOnly even though they do not have any expiration date.

Held for the Browsing Session, Not on Disk Long-Term

Typically, browsers use session cookies and store them in RAM, unlike with cookies that come with a future expiration date, which are stored in persistent storage. However, the specific implementation of such storage can differ across browsers.

Session Restore Can Extend Its Apparent Lifetime

A number of browsers provide the function of reopening tabs from a previous session after a reboot, so that capability will actually allow restoring session cookies despite the fact that the cookie was designed to expire when the browser was shut down. Any website cannot assume that a session cookie indicates an uninterrupted visit if the restore function of a browser has been activated.

Common Use Cases

A session cookie is used to allow a website to recognize the multiple requests coming from a single authorized user. Instead of containing authentication details, it only holds a session ID. The server can identify the session based on that session ID only, thus preventing the actual password from appearing in any of the future requests made by the same user.

Shopping Carts and Multi-Step Forms

Various commercial websites and forms of multiple pages often lean on session cookies to connect everything the website user adds or fills in before being able to complete the last confirmation stage without the requirement for visitors to create an account.

Coordinating Activity Across Open Tabs

Since session cookies belong to the browser instead of a particular tab, any number of tabs opened to the same site usually share the same session. This means visitors can transfer in-progress tasks, such as an order or a form, from one tab to another, as each tab communicates with the server using the same session identifier.

Session Fixation Defenses

Because a session identifier is central to recognizing a visitor, sites that authenticate users are generally advised to issue a new session cookie at the moment of login, even replacing one that already exists. Reissuing the identifier at that point helps prevent an attacker from pre-setting a known session value and waiting for a victim to authenticate under it.

Visible in Developer Tools, Not in Page Source

Even though a session cookie does not appear in the HTML code, it can be seen in developer tools or the network inspector because it comes with every matching request as plain text in an unencrypted connection. This means that a session cookie is very easy to work with in development or debugging, but means that it has to be treated exactly like any other secret data in transit. Server logs showing request headers with cookie values can accidentally contain active sessions along with ordinary traffic logs.

Limitations

The expiration of a session cookie depends on when the browser considers that the session has ended, and this can occur much later than anticipated by the web designer, especially if the session restore option has been active. A website should not assume that the omission of expiry date means that it is a short-lived cookie on practical terms.

Not a Security Mechanism on Its Own

The fact that a cookie is referred to as a session cookie does not imply anything regarding security against attacks (by scripts or unauthorized access); rather, cookie protection is realized through certain cookie properties, separate from the distinction between session cookies and (long-lasting) persistent cookies. The same cookie can be reused after it is intercepted or guessed, unless the server stops accepting it before the cookie expires in the visitor’s client.

Browser Behavior Is Not Uniform

Since the standard relies on the browser to determine what a "session" is, a session cookie may have different meanings depending on which browser is used, or even what settings are chosen in the same browser. Any website that relies heavily on the expiration of session cookies should be aware of that variability rather than assuming that the behavior exhibited in one browser is applicable in all others.

Frequently asked questions

Does Closing One Browser Tab End the Session?

No. A browsing session is generally tied to the browser process as a whole, not to an individual tab, so closing one tab among several open ones does not clear session cookies associated with that browser.

Can JavaScript Read a Session Cookie?

It depends on a separate attribute. A session cookie without the HttpOnly flag can be read by a script running on the page, while one with that flag set cannot, regardless of whether the cookie itself is a session or a permanent one.

Is a Session Cookie the Same Thing as Browser Session Storage?

No. A session cookie is sent to the server with each matching request, while the Web Storage APIs that browsers expose for session-scoped data are accessible only to scripts on the page and are never transmitted automatically.

Can a Site Convert a Session Cookie Into a Permanent One?

Yes. A server can send an updated Set-Cookie header with the same name but with an Expires or Max-Age attribute added, which replaces the existing session cookie with a permanent one going forward. The reverse is also possible by sending the cookie again without those attributes.

Do Session Cookies Count Toward a Site's Total Cookie Storage?

Yes, while they exist. A session cookie occupies the same storage and counts against the same per-domain limits as any other cookie; the distinction only affects when it is removed, not whether it takes up space while active.