Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

The browser warns about a SameSite cookie set from PHP

ErrorA cookie without a SameSite attribute triggers a browser warning in the console

Short answer

A cookie sent without a SameSite attribute gets the browser's default, and a cookie marked None is dropped unless it is also Secure. In PHP 7.3 and later, pass an options array to setcookie() with samesite and secure set. Choose Lax or Strict when the cookie is not needed cross-site.

  • Applies to PHP 8.3, Chrome and Firefox current versions
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the Set-Cookie header the server really sends. It shows whether SameSite and Secure are present.

    curl -s -D - -o /dev/null https://example.com/login | grep -i set-cookie
  2. 2Match what you see, then jump to its fix

    1. 1The Set-Cookie header has no SameSite partsetcookie() is called without a SameSite attributeFix
    2. 2The cookie is blocked in a cross-site embed or requestSameSite=None without the Secure attributeFix
    3. 3Secure cookies never appear on http:// pagesThe Secure attribute is set but the site is plain HTTPFix
    4. 4PHPSESSID is the cookie the browser complains aboutThe PHP session cookie has no SameSite valueFix

What does the error look like?

  • A cookie without a SameSite attribute triggers a browser warning in the console
  • A cookie with SameSite=None is rejected because it lacks the Secure attribute

Cause 1 of 4

setcookie() is called without a SameSite attribute

1Check

If the samesite element is omitted, PHP sets no SameSite attribute, and the browser applies its own default. Some browsers treat a missing value as Lax.

grep -rn "setcookie(" --include=*.php . | head

2Fix

Use the options-array form (PHP 7.3 and later) and set samesite to Lax, Strict or None.

setcookie('prefs', $value, [
    'expires'  => time() + 3600,
    'path'     => '/',
    'secure'   => true,
    'httponly' => true,
    'samesite' => 'Lax',
]);

3Verify

The header should now end with SameSite=Lax.

curl -s -D - -o /dev/null https://example.com/login | grep -i set-cookie

Confirmed by php.net

Did this fix it?

Cause 2 of 4

SameSite=None without the Secure attribute

1Check

None sends the cookie on cross-site requests too, and the Secure attribute must also be set with it. PHP's manual says that with None, a cookie without Secure will be blocked by the client.

curl -s -D - -o /dev/null https://example.com/login | grep -i "samesite"

2Fix

Set secure to true together with samesite None, and serve the site over HTTPS.

setcookie('widget', $value, [
    'path'     => '/',
    'secure'   => true,
    'samesite' => 'None',
]);

3Verify

Reload the page that embeds the cookie and check it is accepted.

curl -s -D - -o /dev/null https://example.com/login | grep -i set-cookie

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 4

The Secure attribute is set but the site is plain HTTP

1Check

A Secure cookie is only sent over https, and an insecure http page cannot set one, except on localhost.

curl -I http://example.com | head -n 1

2Fix

Serve the site over HTTPS. For local work use localhost, which is exempt.

# serve the page over https, or use http://localhost for local work

3Verify

The cookie shows up in the browser's storage panel.

curl -k -s -D - -o /dev/null https://localhost/login | grep -i set-cookie

Confirmed by developer.mozilla.org

Did this fix it?

Cause 4 of 4

The PHP session cookie has no SameSite value

1Check

session.cookie_samesite defaults to an empty string, which means no SameSite attribute is set. The manual lists Lax and Strict for it.

php -r 'echo ini_get("session.cookie_samesite");'

2Fix

Set it in php.ini, or call session_set_cookie_params() with an options array before session_start().

session_set_cookie_params([
    'lifetime' => 0,
    'path'     => '/',
    'secure'   => true,
    'httponly' => true,
    'samesite' => 'Lax',
]);
session_start();

3Verify

The PHPSESSID cookie in the response should now carry SameSite=Lax.

curl -s -D - -o /dev/null https://example.com/login | grep -i phpsessid

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Set SameSite explicitly on every cookie instead of relying on browser defaults.
  • Use Lax or Strict unless a cross-site embed really needs the cookie.
  • Check the Set-Cookie header after every change, not only the browser UI.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Security

Is my PHP application affected by PHP2Shell, and how do I mitigate it?

A scanner or advisory feed reports CVE-2026-109619 (RCE via stale global state) on your server

You are exposed when the PHP serving web requests is older than 8.2.35, 8.3.36, 8.4.27 or 8.5.12 on its branch, or when a distribution package is older than its vendor's fixed build.

6 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.