Skip to main content Skip to search Web HTTP Reference Headers Set-Cookie Set-Cookie header Baseline Widely available * This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015. * Some parts of this feature may have varying levels of support. Learn more See full compatibility The HTTP Set-Cookie response header is used to send a cookie from the server to the user agent, so that the user agent can send it back to the server later. To send multiple cookies, multiple Set-Cookie headers should be sent in the same response. Warning: Browsers block frontend JavaScript code from accessing the Set-Cookie header, as required by the Fetch spec, which defines Set-Cookie as a forbidden response header name that must be filtered out from any response exposed to frontend code. When a Fetch API or XMLHttpRequest API request uses CORS, browsers will ignore Set-Cookie headers present in the server's response unless the request includes credentials. Visit Using the Fetch API - Including credentials and the XMLHttpRequest article to learn how to include credentials. For more information, see the guide on Using HTTP cookies. Header type Response header Forbidden request header No Forbidden response header Yes Syntax http Set-Cookie: = Set-Cookie: =; Domain= Set-Cookie: =; Expires= Set-Cookie: =; HttpOnly Set-Cookie: =; Max-Age= Set-Cookie: =; Partitioned Set-Cookie: =; Path= Set-Cookie: =; Secure Set-Cookie: =; SameSite=Strict Set-Cookie: =; SameSite=Lax Set-Cookie: =; SameSite=None; Secure // Multiple attributes are also possible, for example: Set-Cookie: =; Domain=; Secure; HttpOnly Attributes = Defines the cookie name and its value. A cookie definition begins with a name-value pair. A can contain any US-ASCII characters except for control characters (ASCII characters 0 up to 31 and ASCII character 127) or separator characters (space, tab and the characters: ( ) < > @ , ; : \ " / [ ] ? = { }) A can optionally be wrapped in double quotes and include any US-ASCII character excluding control characters (ASCII characters 0 up to 31 and ASCII character 127), Whitespace, double quotes, commas, semicolons, and backslashes. Encoding: Many implementations perform percent-encoding on cookie values. However, this is not required by the RFC specification. The percent-encoding does help to satisfy the requirements of the characters allowed for . Note: Some cookie names contain prefixes that impose specific restrictions on the cookie's attributes in supporting user-agents. See Cookie prefixes for more information. Domain= Optional Defines the host to which the cookie will be sent. Only the current domain can be set as the value, or a domain of a higher order, unless it is a public suffix. Setting the domain will make the cookie available to it, as well as to all its subdomains. If omitted, the cookie is returned only to the host that sent them (i.e., it becomes a "host-only cookie"). This is more restrictive than setting the host name, as the cookie is not made available to subdomains of the host. Contrary to earlier specifications, leading dots in domain names (.example.com) are ignored. Multiple host/domain values are not allowed, but if a domain is specified, then subdomains are always included. Expires= Optional Indicates the maximum lifetime of the cookie as an HTTP-date timestamp. See Date for the required formatting. If unspecified, the cookie becomes a session cookie. A session finishes when the client shuts down, after which the session cookie is removed. Warning: Many web browsers have a session restore feature that will save all tabs and restore them the next time the browser is used. Session cookies will also be restored, as if the browser was never closed. The Expires attribute is set by the server with a value relative to its own internal clock, which may differ from that of the client browser. Firefox and Chromium-based browsers internally use an expiry (max-age) value that is adjusted to compensate for clock difference, storing and expiring cookies based on the time intended by the server. The adjustment for clock skew is calculated from the value of the DATE header. Note that the specification explains how the attribute should be parsed, but does not indicate if/how the value should be corrected by the recipient. HttpOnly Optional Forbids JavaScript from accessing the cookie, for example, through the Document.cookie property. Note that a cookie that has been created with HttpOnly will still be sent with JavaScript-initiated requests, for example, when calling XMLHttpRequest.send() or fetch(). This mitigates attacks against cross-site scripting (XSS). Max-Age= Optional Indicates the number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately. If both Expires and Max-Age are set, Max-Age has precedence. Partitioned Optional Indicates that the cookie should be stored using partitioned storage. Note that if this is set, the Secure directive must also be set. See Cookies Having Independent Partitioned State (CHIPS) for more details. Path= Optional Indicates the path that must exist in the requested URL for the browser to send the Cookie header. If omitted, this attribute defaults to the path component of the request URL. For example, if a cookie is set by a request to https://example.com/docs/Web/HTTP/index.html, the default path would be /docs/Web/HTTP/. The forward slash (/) character is interpreted as a directory separator, and subdirectories are matched as well. For example, for Path=/docs, the request paths /docs, /docs/, /docs/Web/, and /docs/Web/HTTP will all match. the request paths /, /docsets, /fr/docs will not match. Note: The path attribute lets you control what cookies the browser sends based on the different parts of a site. It is not intended as a security measure, and does not protect against unauthorized reading of the cookie from a different path. SameSite= Optional Controls whether or not a cookie is sent with cross-site requests: that is, requests originating from a different site, including the scheme, from the site that set the cookie. This provides some protection against certain cross-site attacks, including cross-site request forgery (CSRF) attacks. The possible attribute values are: Strict Send the cookie only for requests originating from the same site that set the cookie. Lax Send the cookie only for requests originating from the same site that set the cookie, and for cross-site requests that meet both of the following criteria: The request is a top-level navigation: this essentially means that the request causes the URL shown in the browser's address bar to change. This would exclude, for example, requests made using the fetch() API, or requests for subresources from or