Resolving SameSite Cookie Restrictions in PHP 7.3+
When upgrading to PHP 7.3, the introduction of the SameSite attribute fundamentally changed how session cookies are handled by modern browsers. The default configuration for this new directive is set to Lax. While this provides a baseline for security against cross-site request forgery (CSRF), it inadvertently blocks cookies from being sent in cross-site contexts unless explicitly configured otherwise.
If you are encountering the specific Chrome console error regarding blocked cookiesââA cookie associated with a cross-site resource at URL was set without the SameSite attributeââit indicates that your browser is enforcing strict same-site policies. To resolve this, the SameSite attribute must be explicitly set to None.
However, there is a critical technical constraint often overlooked: setting SameSite=None requires the Secure flag. This means cookies marked with SameSite=None can only be transmitted over HTTPS. Attempting to send such a cookie over HTTP will result in it being rejected by the browser immediately.
Configuring Session Cookies Globally
For applications where you need cross-site cookies for all sessions, the most straightforward approach is to configure this at the server or application level using the php.ini file or environment variables. In PHP 7.3 and later, you can define a global default via the session.cookie_samesite directive.
You must explicitly set this value to None. Note that if you only change session.cookie_samesite, you might still need to ensure the secure flag is enabled depending on your specific environment setup. The configuration usually looks like this:
session.cookie_samesite = None
session.cookie_secure = 1
If you are using a web server configuration (like Apache or Nginx) with PHP-FPM, ensure that these settings align. If session.cookie_secure is set to 0, the browser will ignore the SameSite=None instruction and block the cookie regardless of the header value sent by PHP.
Setting Attributes Programmatically in Laravel
In frameworks like Laravel, you often have fine-grained control over cookie attributes via the Cookie facade or helper functions. To explicitly set a cross-site cookie, you must pass the sameSite option along with ensuring the cookie is marked as secure.
Here is how you might configure a session cookie in a Laravel controller:
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cookie;
$cookie = Cookie::make(
'cross_site_token',
$value,
120, // Minutes
'/', // Path
App::environment() === 'production' ? null : 'localhost', // Domain
true, // HttpOnly
true, // Secure
'None' // SameSite
);
session()->put('cross_site_token', $value);
In this example, the Secure flag is set to true, which is mandatory when SameSite is None. Without the secure flag, the browser will not accept the None setting, and you will continue to see the blocking error in your developer tools.
Understanding Trade-offs and Limitations
Setting SameSite=None effectively disables the protection against CSRF attacks for that specific cookie unless additional measures are taken. The Strict or Lax defaults prevent cookies from being sent during cross-site requests, which is the primary defense mechanism against CSRF. By switching to None, you allow the browser to send the cookie in cross-origin scenarios, but you must ensure your application validates tokens or signatures for any state-changing requests coming from other domains.
It is important to note that this configuration impacts all users visiting your site. If you have a mix of internal and external traffic, setting global defaults to None might not be ideal if those cookies are intended solely for internal session management. In such cases, consider setting the attribute programmatically only when necessary rather than relying on a global php.ini override.
Final Verification
After making these changes, clear your browser's cache and cookies. The most reliable way to verify the fix is to open the Chrome Developer Tools, navigate to the Application tab, and inspect the Cookies storage. Refresh your page and look for any console warnings regarding blocked cookies. If the warning disappears and the cookie value appears in the list, the configuration is working as intended.
