我最近在属性“同一站点”上读到了"RFC 6265“,我看了一些在2016年4月讨论过的文章,”同站点“属性已经为Chrome 51和Opera 39实现了.
我想知道当前的PHP是否支持用这个属性创建cookie?
参考资料:
发布于 2017-10-27 09:04:33
[重要更新:如@caw在下面指出,这个黑客将破坏PHP7.3。现在就停止使用它来避免令人不快的惊喜!或者至少将其包装在PHP版本检查中,比如if (PHP_VERSION_ID < 70300) { ... } else { ... }
。]
似乎您可以滥用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。
发布于 2018-07-02 03:08:12
1. >= v7.3
可以使用$options
数组设置samesite
值,例如:
setcookie($name, $value, [
'expires' => time() + 86400,
'path' => '/',
'domain' => 'domain.example',
'secure' => true,
'httponly' => true,
'samesite' => 'None',
]);
samesite元素的值应该是None
、Lax
或Strict
。
阅读手册页中的更多内容。
2.对于PHP < v7.3
根据您的代码库/需求,您可以使用下列解决方案/解决方案之一
2.1使用设置SameSite cookie
可以将以下行添加到Apache配置中
Header always edit Set-Cookie (.*) "$1; SameSite=Lax"
这将使用SameSite=Lax
标志更新所有cookies。
请参阅这里的更多内容:https://blog.giantgeek.com/?p=1872
2.2使用Nginx配置设置SameSite cookie
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
标志更新所有cookies
请参阅这里的更多内容:https://serverfault.com/questions/849888/add-samesite-to-cookies-using-nginx-as-reverse-proxy
2.3使用SameSite方法设置header
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并不是在等待PHP7.3,而是已经在请看这里下完成了
您也可以在Laravel中使用相同的,因为Laravel在引擎盖下使用Symfony的Symfony\Component\HttpFoundation\Cookie
类
2.4使用SameSite方法中的bug设置setcookie
cookie
setcookie('cookie-name', '1', 0, '/; samesite=strict');
对此要小心,它是PHP7.3版本中已知的setcookie
方法中的一个bug,已在PHP7.3版本中解决,参见此处- https://github.com/php/php-src/commit/5cb825df7251aeb28b297f071c35b227a3949f01
发布于 2020-01-08 22:43:06
基于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://stackoverflow.com/a/46971326/2308553
*
* @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,
]);
}
https://stackoverflow.com/questions/39750906
复制相似问题