关于iframe 中页面无法设置cookie(session)问题
原因是因为浏览器中SameSite默认值为Lax
详细解释见 https://www.ruanyifeng.com/blog/2019/09/cookie-samesite.html
分别尝试以下方法:
1.php代码直接设置
header('Set-Cookie: cross-site-cookie=name; SameSite=None; Secure');
setcookie('cross-site-cookie', 'name', ['samesite' => 'None', 'secure' => true]);
//该方法似乎可行,但是需要项目中有基类,可以统一设置
session_set_cookie_params(['samesite' => 'None', 'secure' => true]);
$currentCookieParams = session_get_cookie_params();
$cookie_domain= 'your domain';
if (PHP_VERSION_ID >= 70300) {
session_set_cookie_params([
'lifetime' => $currentCookieParams["lifetime"],
'path' => '/',
'domain' => $cookie_domain,
'secure' => "1",
'httponly' => "1",
'samesite' => 'None',
]);
} else {
session_set_cookie_params(
$currentCookieParams["lifetime"],
'/; samesite=None',
$cookie_domain,
"1",
"1"
);
}
session_start();
//等待验证
发表回复