I recently read "RFC 6265" on the attribute "Same Site", I looked at some articles that talked about that in April 2016, "same-site" attribute has been implemented for Chrome 51 and Opera 39 ...
I wonder if current PHP supports creating cookies with this attribute?
Reference:
Based on Steffen's answer above, this is the method I am using to support both php <= 7.2 and php >= 7.3:
According to this site, it seems it is a matter of PHP 7.3. As of the voting results, a more general extension to cookie-related functions is being implemented + there might be also a new key in php.ini file.
But as Marc B already wrote, you can use header() function call instead, I would do it in some file with used for inclusion of other initial stuff.
1. For PHP >= v7.3
You will have
$samesite
parameter insetcookie
functionSee more here - PHP RFC: Same Site Cookie
2. For PHP < v7.3
You can use one of the following solutions/workarounds depending on your codebase/needs
2.1 Setting SameSite cookies using Apache configuration
You can add the following line to your Apache configuration
and this will update all your cookies with
SameSite=Lax
flagSee more here: https://blog.giantgeek.com/?p=1872
2.2 Setting SameSite cookies using Nginx configuration
Same here, this also will update all your cookies with
SameSite=Lax
flagSee more here: https://serverfault.com/questions/849888/add-samesite-to-cookies-using-nginx-as-reverse-proxy
2.3 Setting SameSite cookies using
header
methodAs we know cookies are just a header in HTTP request with the following structure
so we can just set the cookies with
header
methodIn fact, Symfony is not waiting for PHP 7.3 and already doing it under the hood, see here
[Important update: As @caw pointed out below, this hack WILL BREAK in PHP 7.3. Stop using it now to save yourself from unpleasant surprises! Or at least wrap it in a PHP version check like
if (PHP_VERSION_ID < 70300) { ... } else { ... }
.]It seems like you can abuse the "path" or "domain" parameter of PHP's "setcookie" function to sneak in the SameSite attribute because PHP does not escape semicolons:
Then PHP sends the following HTTP header:
I've just discovered this a few minutes ago, so please do your own testing! I'm using PHP 7.1.11.