Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

Chrome drops a cookie from another site because third-party cookies are blocked

Your error

This attempt to set a cookie via a Set-Cookie header was blocked due to user preferences3 more lines it can show

Fix it fast

Most common cause, 1 of 3

The front end and the PHP API are on different sites

Put the API on the same site as the front end: a subdomain of the app's own domain, or a path on the app's host that forwards to the PHP backend.

Fix
<?php
// API now served from https://api.example.com for the app on https://app.example.com
setcookie('session', $token, [
    'path'     => '/',
    'secure'   => true,
    'httponly' => true,
    'samesite' => 'Lax',
]);
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

DevTools says a Set-Cookie header was blocked due to user preferences when the cookie comes from another site than the page and the browser blocks third-party cookies, as Chrome does in Incognito by default. SameSite=None; Secure cannot help then. Serve the API on the page's own site, or mark embed cookies Partitioned (CHIPS). Current Chrome.

  • Applies to Chrome current stable (CHIPS since Chrome 114), PHP 8.5 and 8.6 (setcookie partitioned option), PHP 8.2 to 8.4 (header() form)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Open the DevTools console in the frame that sends the request and compare its site with the API's. Different registrable domains (or schemes) make the response cookie third-party; true for embedded means the page runs inside someone else's iframe.

Run this first
({ page: location.origin, api: new URL('https://api.example.net/login').origin, embedded: window.top !== window })

Then find the line you see in this list and open its fix:

  1. 1The front end and the PHP API are on different sitesembedded is false, the page is on one registrable domain (app.example.com) and the API on another (api.example.net), and the cookie is sent from an AJAX or fetch call with credentials
  2. 2A widget embedded on other sites sets its own cookieembedded is true: your PHP app runs in an iframe on a customer's site, and the cookie only needs to work inside that embed
  3. 3An embedded login must share one session across many sitesembedded is true, and the embed needs the same signed-in session it has on its own site, so a cookie stored per top-level site does not help

What does the error look like?

  • This attempt to set a cookie via a Set-Cookie header was blocked due to user preferences
  • This Set-Cookie was blocked due to user preferences.
  • This cookie was blocked due to user preferences.
  • The login works in a normal window but the session cookie never sticks in Incognito

Cause 1 of 3

The front end and the PHP API are on different sites

You seeembedded is false, the page is on one registrable domain (app.example.com) and the API on another (api.example.net), and the cookie is sent from an AJAX or fetch call with credentials

Check

Blocked due to user preferences means the browser itself refused the cookie because it counts as third-party. Two URLs are same-site when the scheme and the eTLD+1 match, so app.example.com and api.example.com are same-site while example.com and example.net are not. Chrome blocks third-party cookies in Incognito by default, and for everyone who picked Block third-party cookies in settings; Google's guide says that setting blocks them in all contexts, so SameSite=None; Secure cannot bring the cookie back.

Check
curl -s -D - -o /dev/null https://api.example.net/login | grep -i '^set-cookie'

Fix

Put the API on the same site as the front end: a subdomain of the app's own domain, or a path on the app's host that forwards to the PHP backend. The cookie is then first-party, and SameSite=Lax is enough.

Fix
<?php
// API now served from https://api.example.com for the app on https://app.example.com
setcookie('session', $token, [
    'path'     => '/',
    'secure'   => true,
    'httponly' => true,
    'samesite' => 'Lax',
]);

Verify

In an Incognito window, the login response's Cookies tab in DevTools shows the cookie with no warning, and the next request sends it.

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

Confirmed by web.dev

Did this fix it?

Cause 2 of 3

A widget embedded on other sites sets its own cookie

You seeembedded is true: your PHP app runs in an iframe on a customer's site, and the cookie only needs to work inside that embed

Check

An iframe from your domain on a customer's page is a third-party context, so its cookies fall under the same block. A Partitioned cookie (CHIPS) is stored per top-level site: the copy set while embedded on site A is sent only while the embed is on site A again. Google's CHIPS guide calls partitioned cookies the way to read and write cookies in cross-site iframes while third-party cookies are blocked. Chrome has supported them by default since version 114, and they must carry Secure.

Check
curl -s -D - -o /dev/null https://widget.example.com/frame | grep -i '^set-cookie'

Fix

Mark the embed's cookie Partitioned. PHP 8.5 added a partitioned option to setcookie(), which demands secure; on older PHP send the header yourself.

Fix
<?php
// PHP 8.5 and later
setcookie('__Host-widget', $id, [
    'path'        => '/',
    'secure'      => true,
    'samesite'    => 'None',
    'partitioned' => true,
]);

// PHP 8.2 to 8.4
header('Set-Cookie: __Host-widget=' . rawurlencode($id) . '; Path=/; Secure; SameSite=None; Partitioned', false);

Verify

In DevTools under Application, Storage, Cookies, the cookie appears with the partition key of the site that embeds the widget.

Verify
curl -s -D - -o /dev/null https://widget.example.com/frame | grep -i 'partitioned'

Confirmed by privacysandbox.google.com

Did this fix it?

Cause 3 of 3

An embedded login must share one session across many sites

You seeembedded is true, and the embed needs the same signed-in session it has on its own site, so a cookie stored per top-level site does not help

Check

Partitioning gives each embedding site its own copy, so it cannot carry a login made on your own domain into another site's page. Google lists the Storage Access API for this case: an iframe asks the user for access to its unpartitioned cookies. MDN notes the request is refused unless it runs during a user gesture such as a click, or permission was granted before.

Check
navigator.permissions.query({ name: 'storage-access' }).then((s) => console.log(s.state))

Fix

Ask for storage access from a button inside the iframe, then reload the embed's state. If the user refuses, fall back to a top-level login page on your own site.

Fix
document.querySelector('#sign-in').addEventListener('click', () => {
  document.requestStorageAccess().then(
    () => location.reload(),
    () => window.open('https://login.example.com/', '_blank'),
  );
});

Verify

After the click and the user's consent, the session cookie is sent with the embed's next request.

Verify
navigator.permissions.query({ name: 'storage-access' }).then((s) => console.log(s.state))   // granted

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Test every cookie-based login in an Incognito window, where Chrome blocks third-party cookies by default.
  • Plan API and front-end hosts on one registrable domain from the start, so session cookies stay first-party.
  • Give each embed cookie a __Host- name with Secure and Partitioned, and keep shared sign-in for a top-level page.

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 on the same stack: Spotless Laundry, EZ Estimater.

Next step

Want this handled for good?

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