How to Set SameSite Cookie Attributes in PHP, Express, .NET

Updated

how to set samesite cookie attribute

Pass the attribute in your framework’s cookie options. PHP: setcookie('session_demo', 'v1', ['samesite' => 'Lax']); Express: res.cookie('name', 'value', { sameSite: 'lax' }); .NET: SameSiteMode.Lax. If you use SameSite=None, pair it with Secure or Chrome, Edge and Firefox drop the cookie. Left unset, it is Lax in Chrome and Edge but not in Firefox or Safari.

Setting SameSite in code

With PHP 8.5.8’s built-in web server, these are the real Set-Cookie headers it sends for three cases: explicit Lax, explicit None with Secure, and no samesite key at all:

setcookie('session_demo', 'v1', ['samesite' => 'Lax', 'path' => '/', 'httponly' => true]);
setcookie('cross_demo', 'v2', ['samesite' => 'None', 'secure' => true, 'path' => '/']);
setcookie('legacy_demo', 'v3', ['path' => '/']); // no samesite key at all

Set-Cookie: session_demo=v1; path=/; HttpOnly; SameSite=Lax
Set-Cookie: cross_demo=v2; path=/; secure; SameSite=None
Set-Cookie: legacy_demo=v3; path=/

Note the third line: PHP does not inject a default SameSite value when you omit the key. Any Lax default comes from the browser on receipt, not from the server, and which browsers apply it is covered below. Always pass 'secure' => true alongside 'samesite' => 'None'; PHP will emit SameSite=None without Secure if you ask it to, and Chrome, Edge and Firefox will then discard the cookie on arrival.

Node (Express): res.cookie('name', 'value', { sameSite: 'lax' }). .NET: options.SameSite = SameSiteMode.Lax; Response.Cookies.Append("name", "value", options);. Same rule in both: None pairs with a secure/HTTPS flag, or Chrome, Edge and Firefox discard the cookie.

What browsers do when SameSite is not set

According to MDN’s browser-compatibility data (checked 2026-10-06), Chrome 80+ and Edge 86+ treat a cookie with no SameSite attribute as Lax, Firefox does so only when its network.cookie.sameSite.laxByDefault preference is switched on, and Safari does not apply the default. Chrome’s change rolled out from February through August 2020. MDN’s cookie reference states the default without naming browsers: “if no SameSite attribute is set, the cookie is treated as Lax by default”.

SameSite=None; Secure means the cookie travels on both first-party and cross-site requests but only over HTTPS. Omit Secure and Chrome 80+, Edge 86+ and Firefox 131+ drop it outright; Safari does not enforce the rule, per the same MDN data.

Setting nothing is not quite identical to setting Lax explicitly, which is the part most explanations miss. As of 15 October 2026, the main branch of Chromium’s source sets a temporary compatibility allowance for unset-SameSite cookies younger than two minutes on cross-site POST requests, and declares the two features that shorten or remove it disabled by default (I read the source, not shipped Chrome builds); an explicit SameSite=Lax cookie gets no such allowance. If you want the strict behaviour from the first request, set it explicitly. For the deeper, cross-browser measured behavior of each SameSite value, including that two-minute window, see what SameSite cookies are and how each engine actually handles them. This page stays focused on setting the attribute in code.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.