Skip to content

Core\Response::SameSite

Which cross-site requests carry your cookie. A cookie belongs to your site, and this setting decides whether it also travels on a request that somebody else's page made to yours.

Core\Response::SameSite is declared in the class Core\Response. The method addCookie takes it as its sameSite option.

Lax sends it when a visitor clicks through to your site, and with nothing else — not with an image, a form post or a script call from elsewhere. That is what a cookie you say nothing about gets. Strict never sends it across a site boundary at all, so somebody arriving from a link arrives signed out. None sends it from anywhere, and a cookie that travels that far has to be marked secure as well; the pair without it is refused, because a browser drops such a cookie rather than storing it.

Good to know: this is the setting a cross-site request forgery has to get past. A form on somebody else's page can still post to yours; what Lax stops is that post arriving with your visitor's session.

Examples

The three choices and what each one sends

<?nvs
// The three choices, and what a cookie carrying each one is sent with. A
// request counts as cross-site when the page making it is not yours: a link on
// it, a form on it, or a script on it calling your server.
class Travel
{
public static function label(Core\Response::SameSite $choice): string
{
return match ($choice) {
Core\Response::SameSite::Lax => 'Lax ',
Core\Response::SameSite::Strict => 'Strict',
Core\Response::SameSite::None => 'None ',
};
}
// Somebody on another site clicks a link to one of your pages.
public static function onAClickFromElsewhere(Core\Response::SameSite $choice): bool
{
return match ($choice) {
Core\Response::SameSite::Lax => true,
Core\Response::SameSite::Strict => false,
Core\Response::SameSite::None => true,
};
}
// Another site's page posts a form to you, or asks your server for
// something from a script. This is the one that matters for safety.
public static function onAPostFromElsewhere(Core\Response::SameSite $choice): bool
{
return match ($choice) {
Core\Response::SameSite::Lax => false,
Core\Response::SameSite::Strict => false,
Core\Response::SameSite::None => true,
};
}
}
array<Core\Response::SameSite> $choices = [
Core\Response::SameSite::Lax,
Core\Response::SameSite::Strict,
Core\Response::SameSite::None,
];
foreach ($choices as Core\Response::SameSite $choice) {
echo Travel::label($choice),
' click from another site: ', Travel::onAClickFromElsewhere($choice) ? 'yes' : 'no ',
' post from another site: ', Travel::onAPostFromElsewhere($choice) ? 'yes' : 'no',
"\n";
}
Output
Lax click from another site: yes post from another site: no
Strict click from another site: no post from another site: no
None click from another site: yes post from another site: yes

Picking the choice for what the cookie is for

<?nvs
// This program sets three cookies for three different uses.
// Each cookie has a different `sameSite` setting.
// A cookie for a signed-in session. It has no `sameSite` setting,
// so it gets the default, which is `Lax`.
Core\Response::addCookie('session', 's-88f1');
echo "session: nothing written, so it is Lax\n";
// With `Strict`, the browser sends the cookie only on requests from your own site.
// A link from another site to the admin area arrives signed out.
Core\Response::addCookie('admin', 'a-7', { sameSite: Core\Response::SameSite::Strict });
echo "admin: Strict, so a link from another site arrives signed out\n";
// A widget that other sites embed needs its cookie on their requests too.
// That needs `None`, and `None` needs `secure: true`.
Core\Response::addCookie('widget', 'w-9', { sameSite: Core\Response::SameSite::None, secure: true });
echo "widget: None with secure, so an embedded page still gets it\n";
// `None` without `secure: true` throws an error. A browser would not store
// that cookie, so the error tells you now.
try {
Core\Response::addCookie('widget', 'w-9', { sameSite: Core\Response::SameSite::None, secure: false });
} catch (Throwable $dropped) {
echo $dropped->message, "\n";
}
Output
session: nothing written, so it is Lax
admin: Strict, so a link from another site arrives signed out
widget: None with secure, so an embedded page still gets it
Core\Response::addCookie(): `sameSite: SameSite::None` needs `secure: true`, and this cookie has `secure: false` — a browser drops the pair rather than honouring it, so the cookie would never be stored

A payment that comes back from another site

<?nvs
// This program shows the cookies for a checkout with an outside payment provider.
// The provider's page posts the result back to your site. That post comes from
// another site, so the browser does not send the `Lax` sign-in cookie with it.
// A second cookie with `None` carries the order number instead.
string $order = 'ORD-1042';
// The sign-in cookie uses the default, `Lax`. A link from another site arrives signed in.
// A form on another site that posts to you does not get this cookie.
Core\Response::addCookie('session', 's-88f1');
echo "session: Lax, so the provider's post does not carry it\n";
// This cookie contains only the order number. `None` lets it arrive with the provider's post.
// `None` needs `secure: true`, and `maxAge: 30m` makes it expire after 30 minutes.
Core\Response::addCookie('checkout', $order, { sameSite: Core\Response::SameSite::None, secure: true, maxAge: 30m });
echo "checkout: None with secure, so the provider's post does carry it\n";
// After the payment, the checkout cookie is not needed. `maxAge: 0s`
// tells the browser to delete it.
Core\Response::addCookie('checkout', '', { sameSite: Core\Response::SameSite::None, secure: true, maxAge: 0s });
echo 'checkout: taken back once ', $order, " was paid for\n";
Output
session: Lax, so the provider's post does not carry it
checkout: None with secure, so the provider's post does carry it
checkout: taken back once ORD-1042 was paid for