Chrome 80 , same-site屬性導致站點無法傳輸cookies的解決辦法

最近在用的一個項目,忽然出現iframe嵌套的第三方頁面登錄頁面無法使用的情況,在js的日誌中發現:

cookie associated with a cross-site resource at ***** was set without the `SameSite` attribute. It has been blocked, as Chrome now only delivers cookies with cross-site requests if they are set with `SameSite=None` and `Secure`.


Chrome 80 , same-site屬性導致站點無法傳輸cookies的解決辦法


後來通過搜索,發現了網上的幾種解決方法,以下以php為例


PHP >= v7.3


<code>setcookie($name, $value, [
'expires' => time() + 86400,
'path' => '/',
'secure' => true,
'samesite' => 'None',
]);/<code>

For PHP < v7.3


  1. 使用apache設置SameSite cookies(推薦)

在apache的配置文件httpd.conf中添加:

<code>Header always edit Set-Cookie (.*) "$1; Secure; SameSite=None"/<code>

2.在Nginx的配置文件中添加:

<code>location / {
# your usual config ...
# hack, set all cookies to secure, httponly and samesite (strict or lax)
proxy_cookie_path / "/; secure; HttpOnly; SameSite=lax";
}/<code>

3.通過php的header方法添加:

<code>header("Set-Cookie: key=value; path=/; domain=example.org; HttpOnly; SameSite=Lax");/<code>

4.通過php的setcookie方法:

<code>setcookie('cookie-name', '1', 0, '/; samesite=strict');/<code>


分享到:


相關文章: