Complete Guide to Fixing "Set SameSite Cookie to None" Warnings in Chrome Extensions

Nov 24, 2025 · Programming · 8 views · 7.8

Keywords: SameSite Cookie | Chrome Extension | PHP setcookie | Cross-Site Request | Secure Attribute

Abstract: This article provides an in-depth analysis of the "SameSite Cookie not set" warning in Chrome browsers, focusing on solutions for handling cross-site cookies in Chrome extensions using PHP. It offers specific code implementations for PHP versions 7.2, 7.3, and 7.4, including correct parameter configuration for the setcookie function, the necessity of the Secure flag, and how to verify cookie settings in developer tools. The article also explains the three modes of the SameSite attribute (None, Lax, Strict) and their applications in cross-site requests, helping developers fully understand and resolve this common browser compatibility issue.

Problem Background and Warning Analysis

When developing Chrome extensions and calling PHP scripts via popup.js to read cookies, developers often encounter warning messages in the browser console: "A cookie associated with a cross-site resource was set without the SameSite attribute." This warning stems from Chrome's enhanced security requirements for cookies, aimed at preventing cross-site request forgery (CSRF) attacks. According to Chromium project updates, future versions will only deliver cross-site cookies if they are marked with SameSite=None and Secure attributes.

Correct Methods for Setting SameSite Cookies in PHP

The approach to setting SameSite cookies varies depending on the PHP version. Below are specific implementations based on best practices.

PHP 7.3 and Above

Starting from PHP 7.3, the setcookie function supports passing options as an array, making the setup of SameSite attributes more intuitive and standardized. Example code:

setcookie('cors-cookie', 'my-site-cookie', [
  'expires' => time() + 60*60*24*30,
  'path' => '/',
  'domain' => '.example.com',
  'secure' => true,
  'httponly' => true,
  'samesite' => 'None'
]);

In this example, the expires parameter defines the cookie's validity period, path and domain specify the cookie's scope, secure ensures the cookie is transmitted only over HTTPS, httponly prevents client-side script access, and samesite is set to None to allow cross-site requests.

PHP 7.2 and Below

For PHP 7.2 or earlier versions, since the setcookie function does not support array options, the SameSite attribute must be embedded via the path parameter. Implementation as follows:

setcookie('key', 'value', time() + (7*24*3600), "/; SameSite=None; Secure");

Although this method is effective, attention must be paid to parameter order and string format to avoid parsing errors. In practice, upgrading PHP to use the safer array syntax is recommended.

Enhanced Support in PHP 7.4

PHP 7.4 further optimizes cookie handling, supporting more flexible option configurations. Here is a complete example for scenarios requiring fine-grained control over cookie attributes:

$cookie_options = array(
  'expires' => time() + 60*60*24*30,
  'path' => '/',
  'domain' => '.example.com',
  'secure' => true,
  'httponly' => false,
  'samesite' => 'None'
);
setcookie('cors-cookie', 'my-site-cookie', $cookie_options);

This code defines detailed parameters for the cookie, including a 30-day expiration, root path, subdomain support, and secure transmission, ensuring proper functionality in cross-site environments.

Detailed Explanation of SameSite Attribute and Security Considerations

The SameSite attribute controls how cookies are sent in cross-site requests, with three modes:

In the context of Chrome extensions, where requests may involve different domains, setting SameSite to None is often necessary. However, it is crucial to note that SameSite=None must be used with Secure=true; otherwise, the browser will reject the cookie. This requirement arises from Chromium's security policy updates, designed to prevent man-in-the-middle attacks.

Debugging and Verification Methods

To ensure correct cookie settings, developers can use Chrome Developer Tools for verification. Specific steps include:

  1. Open Developer Tools (F12) and navigate to the Application tab.
  2. Under Storage, select Cookies to view the list of cookies for the current domain.
  3. Check if the SameSite and Secure attributes of the target cookie are correctly set.

Additionally, enabling the experimental Chrome flag chrome://flags/#cookie-deprecation-messages can provide more detailed warning messages, aiding in identifying potential cookie issues.

Common Errors and Solutions

Developers often encounter the following issues in practice:

Referencing official Chromium documentation and community examples, such as the SameSite example code from GoogleChromeLabs, can further optimize implementations.

Summary and Best Practices

Resolving SameSite cookie warnings in Chrome extensions hinges on correctly configuring the parameters of PHP's setcookie function. For modern PHP versions (7.3+), using array option syntax is recommended; for older versions, embedding attributes via the path parameter is necessary. Regardless of the method, it is essential to set both SameSite=None and Secure=true to comply with browser security requirements. Regularly inspecting cookie attributes in developer tools and staying updated with official documentation can effectively prevent compatibility issues and enhance application security.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.