PHP setcookie “SameSite=Strict”?

后端 未结 9 2150
旧时难觅i
旧时难觅i 2020-11-29 00:26

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

9条回答
  •  感情败类
    2020-11-29 01:01

    [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:

    setcookie('samesite-test', '1', 0, '/; samesite=strict');
    

    Then PHP sends the following HTTP header:

    Set-Cookie: samesite-test=1; path=/; samesite=strict

    I've just discovered this a few minutes ago, so please do your own testing! I'm using PHP 7.1.11.

提交回复
热议问题