問題描述
我創建了一個 chrome 擴展,并從 popup.js 調用讀取 cookie 的 PHP 腳本(使用 Xhttprequest).像這樣:
I created a chrome extension and from popup.js I called PHP script (Using Xhttprequest) that reads the cookie. Like this:
$cookie_name = "mycookie";
if(isset($_COOKIE[$cookie_name]))
{
echo $_COOKIE[$cookie_name];
}
else{
echo "nocookie";
}
但是我在擴展中的錯誤時收到此警告.
But I'm getting this warning at errors in extensions.
與 (Here is my domain) 的跨站點資源關聯的 cookie 設置為沒有 SameSite
屬性.如果使用 SameSite=None
和 Secure
進行設置,Chrome 的未來版本將僅提供具有跨站點請求的 cookie.您可以在應用程序>存儲>Cookies 下的開發人員工具中查看 cookie,并在 https://www.chromestatus 上查看更多詳細信息.com/feature/5088147346030592 和 https://www.chromestatus.com/feature/5633521622188032.
A cookie associated with a cross-site resource at (Here is my domain) was set without the
SameSite
attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set withSameSite=None
andSecure
. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
我嘗試創建這樣的 cookie,但沒有幫助.
I tried to create a cookie like this but it didn't help.
setcookie($cookie_name,$cookie_value, time() + 3600*24, "/;samesite=None ","mydomain.com", 1);
按照這個問題中的說明進行操作.
Following instructions from this question.
推薦答案
我也在反復試驗"中為此,但是來自 Google Chrome Labs 的 Github 的這個答案對我有所幫助.我將它定義到我的主文件中并且它工作正常 - 很好,僅適用于一個第三方域.仍在進行測試,但我很想用更好的解決方案更新這個答案:)
I'm also in a "trial and error" for that, but this answer from Google Chrome Labs' Github helped me a little. I defined it into my main file and it worked - well, for only one third-party domain. Still making tests, but I'm eager to update this answer with a better solution :)
我現在使用的是 PHP 7.4,此語法運行良好(2020 年 9 月):
I'm using PHP 7.4 now, and this syntax is working good (Sept 2020):
$cookie_options = array(
'expires' => time() + 60*60*24*30,
'path' => '/',
'domain' => '.domain.com', // leading dot for compatibility or use subdomain
'secure' => true, // or false
'httponly' => false, // or false
'samesite' => 'None' // None || Lax || Strict
);
setcookie('cors-cookie', 'my-site-cookie', $cookie_options);
如果您有 PHP 7.2 或更低版本(正如羅伯特在下面回答的那樣):
If you have PHP 7.2 or lower (as Robert's answered below):
setcookie('key', 'value', time()+(7*24*3600), "/; SameSite=None; Secure");
如果您的主機已經更新到 PHP 7.3,您可以使用(感謝 Mahn 的評論):
If your host is already updated to PHP 7.3, you can use (thanks to Mahn's comment):
setcookie('cookieName', 'cookieValue', [
'expires' => time()+(7*24*3600,
'path' => '/',
'domain' => 'domain.com',
'samesite' => 'None',
'secure' => true,
'httponly' => true
]);
您可以嘗試檢查 cookie 的另一件事是啟用下面的標志,用他們自己的話來說,將為可能受此更改影響的每個 cookie 添加控制臺警告消息":
Another thing you can try to check the cookies, is to enable the flag below, which—in their own words—"will add console warning messages for every single cookie potentially affected by this change":
chrome://flags/#cookie-deprecation-messages
查看完整代碼:https://github.com/GoogleChromeLabs/samesite-examples/blob/master/php.md,他們也有 same-site-cookies
的代碼.
See the whole code at: https://github.com/GoogleChromeLabs/samesite-examples/blob/master/php.md, they have the code for same-site-cookies
too.
這篇關于如何修復“將 SameSite cookie 設置為無"警告?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!