PHP setcookie "SameSite=Strict"?

PHP setcookie "SameSite=Strict"?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP setcookie "SameSite=Strict"?

The SameSite attribute, standardized in RFC 6265 and later refined in subsequent drafts, fundamentally changes how browsers handle cross-site requests. Chrome implemented support for this attribute starting in version 51, initially with a default behavior of Lax. The core question developers face today is whether PHP provides a direct mechanism to set this attribute using the native setcookie() function or similar APIs.

The short answer is yes, but only through explicit configuration within the header string passed to setcookie(). Unlike JavaScript's document.cookie API, which handles these attributes automatically in modern environments, PHP requires you to be the architect of every cookie header sent to the client. This responsibility offers granular control but demands precision in implementation.

To set a SameSite=Strict cookie in vanilla PHP, you must include the attribute as part of the third argument of setcookie(). The syntax is straightforward:

<?php
$name = 'session_token';
$value = bin2hex(random_bytes(32));
$options = [
    'expires' => time() + 31536000, // 1 year
    'path' => '/',
    'secure' => true,
    'httponly' => true,
    'samesite' => 'Strict'
];

setcookie($name, $value, $options);

In this example, the 'samesite' key explicitly tells the browser to treat requests originating from other domains as cross-site. When SameSite=Strict is active, third-party sites cannot send cookies back to your domain via cross-origin requests, and even simple GET requests from external pages to yours will not include the cookie. This effectively breaks many traditional cross-site tracking patterns and forces developers to rely on tokens passed in the URL query string or headers for authentication when interacting with third parties.

It is critical to note that PHP's setcookie() function expects the attribute value as a simple string. While RFC 6265 defines Strict and Lax as valid values, early drafts of the specification also supported None. Setting it to 'None' requires that the cookie be marked with the Secure flag; otherwise, most browsers will ignore the attribute entirely or treat it as an error.

The decision to use Strict versus Lax represents a significant trade-off between security and usability. SameSite=Strict prevents all cross-site requests, including top-level navigations and form submissions, which can break legitimate workflows if your application relies on cookies for session management across multiple subdomains or partner sites. Conversely, SameSite=Lax allows the cookie to be sent with GET requests (like navigating to a new page) but blocks it with POST requests (like submitting a form). For most modern web applications aiming to mitigate CSRF attacks without breaking third-party integrations, Lax is often the pragmatic choice unless strict isolation is required.

In the Laravel ecosystem, this logic is abstracted slightly but remains consistent with the underlying HTTP protocol. The Cookie::queue() helper allows you to define these attributes cleanly:

use Illuminate\Support\Facades\Cookie;

Cookie::queue('session', $token, [
    'expires' => now()->addYear(),
    'secure' => env('APP_DEBUG') === false,
    'httpOnly' => true,
    'sameSite' => 'Strict', // or 'Lax'
]);

Under the hood, Laravel constructs the Set-Cookie header exactly as defined in the PHP specification. If you are maintaining legacy code where setcookie() was called with just a name and value, adding the third parameter is mandatory to gain access to these privacy controls. Failing to do so leaves your application vulnerable to cross-site request forgery (CSRF) attacks, especially if you are not relying on CSRF tokens for every state-changing request.

The landscape of cookie security continues to evolve. As browsers move toward stricter defaults and new standards like the HTTPbis draft on first-party cookies emerge, the ability to explicitly declare SameSite behavior is no longer optional; it is a necessity. Whether you are writing raw PHP or using a framework like Laravel, ensuring that your session management strategy accounts for these attributes is essential for maintaining both security and functional integrity in a modern web environment.

Our network