PHP 中跨 HTTP 和 HTTPS 的 Cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2321224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:02:01  来源:igfitidea点击:

Cookie across HTTP and HTTPS in PHP

phphttpcookieshttps

提问by Paulo Coghi - Reinstate Monica

How can I set a cookie in PHP that is readable both in HTTP and HTTPS?

如何在 PHP 中设置一个在 HTTP 和 HTTPS 中都可读的 cookie?

If this isn't possible, what can be done? Set two cookies?

如果这是不可能的,可以做什么?设置两个饼干?

回答by Andrew Strong

By default, a cookie can be read by both http and https at the same URL.

默认情况下,同一 URL 上的 http 和 https 都可以读取 cookie。

However, a server can optionally specify the 'secure' flag while setting a cookie this tells the browser to only send it over a secure channel, such as an SSL connection.

但是,服务器可以选择在设置 cookie 时指定“安全”标志,这会告诉浏览器仅通过安全通道(例如 SSL 连接)发送它。

In this case the cookie will only be sent over https. A cookie not marked as secure will be sent over both http and https.

在这种情况下,cookie 将仅通过 https 发送。未标记为安全的 cookie 将通过 http 和 https 发送。

回答by David Pfeffer

Assuming your domain name remains the same except for the resource type, cookies in PHP (or any language) can be read from both HTTP and HTTPS.

假设您的域名除资源类型外保持不变,则可以从 HTTP 和 HTTPS 读取 PHP(或任何语言)中的 cookie。

e.g.:

例如:

http://www.example.com
https://www.example.com

In this example, the cookies will be readable from each other.

在此示例中,cookie 将可以相互读取。