我最近阅读了关于“Same Site”属性的“RFC 6265”,我查看了一些文章,这些文章讲述了在2016年4月,“same-site”属性已经被Chrome 51和Opera 39实现...
我想知道当前的PHP是否支持创建带有这个属性的cookie?
参考:
我最近阅读了关于“Same Site”属性的“RFC 6265”,我查看了一些文章,这些文章讲述了在2016年4月,“same-site”属性已经被Chrome 51和Opera 39实现...
我想知道当前的PHP是否支持创建带有这个属性的cookie?
参考:
您可以使用$options
数组来设置samesite
值,例如:
setcookie($name, $value, [
'expires' => time() + 86400,
'path' => '/',
'domain' => 'domain.example',
'secure' => true,
'httponly' => true,
'samesite' => 'None',
]);
samesite元素的值应为None
、Lax
或Strict
之一。
请在手册页面中了解更多信息。
您可以根据您的代码库/需要使用以下解决方案/变通方法之一:
您可以将以下行添加到您的Apache配置中
Header always edit Set-Cookie (.*) "$1; SameSite=Lax"
并且这将使用 SameSite=Lax
标志更新您的所有 cookie
在此处查看更多信息:https://blog.giantgeek.com/?p=1872
location / {
# your usual config ...
# hack, set all cookies to secure, httponly and samesite (strict or lax)
proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
}
同样的情况也会更新你所有带有SameSite=Lax
标志的cookie。
更多信息请参见:https://serverfault.com/questions/849888/add-samesite-to-cookies-using-nginx-as-reverse-proxy
header
方法设置SameSite cookie我们知道,cookie只是HTTP请求中的一个标题,具有以下结构
Set-Cookie: key=value; path=/; domain=example.org; HttpOnly; SameSite=Lax
所以我们可以使用header
方法来设置cookie
header("Set-Cookie: key=value; path=/; domain=example.org; HttpOnly; SameSite=Lax");
事实上,Symfony并不需要等待PHP 7.3版本,它已经在幕后做到了,请看这里
Laravel也可以使用相同的方法,因为Laravel在幕后使用Symfony的Symfony\Component\HttpFoundation\Cookie
类。
setcookie
方法中的漏洞设置SameSite cookiesetcookie('cookie-name', '1', 0, '/; samesite=strict');
小心处理这个问题,PHP的setcookie
方法中存在一个已知的错误,在PHP7.3版本中已经得到解决,查看这里 - https://github.com/php/php-src/commit/5cb825df7251aeb28b297f071c35b227a3949f01
setcookie
(和 setrawcookie
)的正确方法签名似乎不是这样的。需要作为第三个参数传递一个选项数组,其中之一称为samesite
。请参见 https://github.com/php/php-src/blob/PHP-7.3/UPGRADING#L350。 - rink.attendant.6session_set_cookie_params()
是否允许将 samesite 注入到 $path 中? - marcovtwoutsession_set_cookie_params()
中的注入对我也起作用,并且我没有在 PHP 变更日志中找到解决方法,这对我来说是可以接受的 :) - malisokan[重要更新:正如@caw在下面指出的那样,这个黑客技巧在PHP 7.3中将会失效。立即停止使用它以避免不愉快的惊喜!或者至少像if (PHP_VERSION_ID < 70300) { ... } else { ... }
这样,在PHP版本检查中包装它。]
看起来你可以滥用PHP的“setcookie”函数的“path”或“domain”参数来窃取SameSite属性,因为PHP不会对分号进行转义:
setcookie('samesite-test', '1', 0, '/; samesite=strict');
然后PHP发送以下HTTP头:
Set-Cookie: samesite-test=1; path=/; samesite=strict
我刚在几分钟前发现了这个问题,请自行测试!我使用的是PHP 7.1.11。
setcookie
方法,这可能是 PHP 7.3,正如其他答案中所述。 - caw基于上面Steffen的答案,这是我使用的方法来支持php <= 7.2和php >= 7.3:
/**
* Support samesite cookie flag in both php 7.2 (current production) and php >= 7.3 (when we get there)
* From: https://github.com/GoogleChromeLabs/samesite-examples/blob/master/php.md and https://dev59.com/FFkS5IYBdhLWcg3wgXHf#46971326
*
* @see https://www.php.net/manual/en/function.setcookie.php
*
* @param string $name
* @param string $value
* @param int $expire
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httponly
* @param string $samesite
* @return void
*/
function setCookieSameSite(
string $name, string $value,
int $expire, string $path, string $domain,
bool $secure, bool $httponly, string $samesite = 'None'
): void {
if (PHP_VERSION_ID < 70300) {
setcookie($name, $value, $expire, $path . '; samesite=' . $samesite, $domain, $secure, $httponly);
return;
}
setcookie($name, $value, [
'expires' => $expire,
'path' => $path,
'domain' => $domain,
'samesite' => $samesite,
'secure' => $secure,
'httponly' => $httponly,
]);
}
if (version_compare(phpversion(),'7.3','<')) { ... }
。 - kenorbHere is the usage:
//set samesite strict php cookie
SameSiteCookieSetter::setcookie('samesite_test','testvalue', array('samesite' => 'Strict'));
if(isset($_COOKIE["PHPSESSID"])){
header('Set-Cookie: PHPSESSID='.$_COOKIE["PHPSESSID"].'; SameSite=None');
}
对我有用,在尝试了所有方法之后(徒劳无功)
session_start()
中指定选项? - theking2补充 Marty Aghajanyan 的答案(因为显然我可以回答,但还不能评论)
在 Apache 2.4.29 (Ubuntu) 中,通过 mod_headers 和 PHP 进行操作并不起作用。在查看文档(http://www.balkangreenfoundation.org/manual/en/mod/mod_headers.html)时,我注意到“always”条件在某些情况下无法从相同的响应头池中工作。因此,在我的情况下,以下设置 SameSite 参数有效。(尽管我在设置最近的 Chrome 80 更新时将其设置为 None)
Header edit Set-Cookie ^(.*)$ "$1; Secure; SameSite=None"
https://www.thinktecture.com/en/identity/samesite/samesite-in-a-nutshell/
有很多示例展示了如何设置这个属性,但很少有解释为什么要这样做。
https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#SameSite_attribute
如果需要发送跨域cookie,请使用None指令来退出SameSite限制。None指令要求同时使用Secure属性。samesite
属性设置secure
和httponly
。因此,在session_start()
之前必须使用所有ini设置。
示例:
ini_set('session.cookie_samesite', 'None');
ini_set('session.cookie_secure', 'On');
ini_set('session.cookie_httponly', 'On');
session_start();
header('Set-Cookie: ...')
。 - Marc B