doc-en icon indicating copy to clipboard operation
doc-en copied to clipboard

setcookie() 'samesite' option bugging

Open Heraes-git opened this issue 4 months ago • 5 comments

From manual page: https://php.net/function.setcookie


This works :

$cookie_options = array('expires' => $max_expiration, 'path' => '/', 'domain' => '', 'secure' => false, 'httponly' => false);
setcookie("Sondage01", $cookie_value_string, $cookie_options);

This doesn't work :

$cookie_options = array('expires' => $max_expiration, 'path' => '/', 'domain' => '', 'secure' => false, 'httponly' => false, 'samesite' => 'None');
setcookie("Sondage01", $cookie_value_string, $cookie_options);

Test page here : https://pastebin.com/5Bu8G225 Uncomment line 5 to make it work again.

Heraes-git avatar Sep 11 '25 11:09 Heraes-git

I add that if you don't use an array but directly fill the options in the setcookie() function, it refuses to have more than 7 arguments (name + value + the 5 options from expires to httponly).

Heraes-git avatar Sep 12 '25 15:09 Heraes-git

What version of PHP are you testing this on?

The behavior you describe sounds like you're using PHP < 7.3, when the $options parameter and samesite support were added, and have warnings and notices disabled. (See changelog on the manual page)

AllenJB avatar Oct 07 '25 16:10 AllenJB

@AllenJB I'm on v8.3.8.

Heraes-git avatar Oct 11 '25 13:10 Heraes-git

I've run OP's pastebin script on PHP 8.4 and it works as expected (and documented). In the Chrome Dev Tools Network tab, if I click on the request and check the 'Headers' tab it shows the Set-Cookie line was sent, with Same-Site=None.

However Chrome blocks the cookie because it does not have the "Secure" attribute. This RFC requirement is noted in the MDN documentation for SameSite

PHP is doing exactly what you asked it to and sending a cookie with SameSite=None and no Secure attribute, but the client is blocking that.

I've suggested adding a note to the setcookie() documentation, but there's no bug here.

AllenJB avatar Oct 11 '25 15:10 AllenJB

Ok, thank you, I take note of this. My bad.

Heraes-git avatar Oct 11 '25 22:10 Heraes-git