PHP SameSite session 问题, session 不起作用

标签 php session cookies samesite

我希望有人能给我一些关于我的问题的想法。我正在尝试应用 SameSite cookie 来使 session 工作,但它似乎不起作用。访问的网站html:

<iframe src="https://www.example.com/test/iframe.php"></iframe>

iframe 源站点:
    <?php
    header('Set-Cookie: cross-site-cookie=PHPSESSID; SameSite=None; Secure');
    session_start();
    if(!isset($_SESSION['test'])){
        echo 1;
        $_SESSION['test'] = 'ee2';
    }else{
        echo $_SESSION['test'];
    }

如果我访问该网站,我仍然收到 A cookie associated with a cross-site resource at https://www.example.com/ 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.浏览器控制台中的消息和 session 未保存。

奇怪的是,cookie实际上已经设置好了:
enter image description here

我错过了什么吗?如果设置了跨站点 cookie,为什么我会在控制台中收到此消息, session 不工作的原因可能是什么?我正在使用 php 7.1.33。如果我直接打开 iframe,它可以正常工作,并且如果我使用浏览器打开站点,但在默认情况下没有启用 SameSite cookie 标志进行测试,它也可以正常工作。

最佳答案

我通过编辑 .htaccess 解决了它

<ifmodule mod_headers.c>
Header always edit Set-Cookie ^(.*)$ $1;SameSite=None;Secure
</ifmodule> 

关于PHP SameSite session 问题, session 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60157086/

相关文章:

php - Distinct 在 mysql 的选择查询中不起作用

PHP MVC 沙箱

php - 在cakephp中访问相关模型/表进行内连接查询

java - Jsf Netbeans、Hibernate、获取 session 工厂找不到符号错误

django - 如何在 Django 中设置或获取 cookie 值

php - 我们可以使用 javascript 检索和设置 PHP cookie 吗

php - PyroCMS/Codeigniter 数据库错误

javascript - 如何在嵌入文件中使用 php session ?

Apache 中的 PHP session

Angular 4 基于 cookie 的身份验证