站点的 HTTP 和 HTTPS 部分之间的 Cookie 不同步

发布于 2024-12-12 07:19:50 字数 278 浏览 0 评论 0原文

我正在使用 CakePHP (v 1.3) 和 Auth 组件。现在,所有 cookie 都不在 HTTP 和 HTTPS 之间的 snyc 中。

例如,用户可以通过HTTP登录而未通过HTTPS登录,或者更糟糕的是,用户A可以通过HTTP登录而用户B可以通过HTTPS登录。

其他事情也会发生这种情况(例如购物车中的商品),

我不知道发生了什么或如何解决它。

我需要做什么才能让它们在 HTTP 和 HTTPS 上拥有相同的 cookie?

I am using CakePHP (v 1.3), and the Auth component. Right now all the cookies are not in snyc between HTTP and HTTPS.

For example, a user can be logged in on HTTP and not logged in on HTTPS or even worse user A can be logged in on HTTP and user B can be logged in on HTTPS.

This is happening for other things as well (like items in cart)

I have no idea what is going on or how to fix it.

What do I need to do so that they have the same cookies on HTTP and HTTPS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿得七秒忆 2024-12-19 07:19:50

这可能是由安全 cookie 引起的,该 cookie 仅在通过 https 访问页面时发送。如果通过 https 访问页面,CakePHP 会自动设置 session.cookie_secure。

要禁用此行为,请将 cake\libs 内的 ini_set('session.cookie_secure', 1); 更改为 ini_set('session.cookie_secure', 0); \cake_session.php

我建议在应用程序端处理它,并允许仅通过 https 登录(和所有会员页面)。

This is probably caused by secure cookies, which are sent only when page is accessed over https. If page is accessed over https, CakePHP sets session.cookie_secure automatically.

To disable this behaviour, change ini_set('session.cookie_secure', 1); to ini_set('session.cookie_secure', 0); inside cake\libs\cake_session.php

I would recommend to deal with it at the application side and allow logging in (and all member pages) only over https.

风吹雨成花 2024-12-19 07:19:50

修改核心代码大多数时候是一件坏事,在你的情况下,有可能做你需要的事情,但蛋糕的方式。

在 app/config/core.php 中:修改 Session.save

//Configure::write('Session.save', 'php');

//this will look for a 'mysession.php' file in app/config
Configure::write('Session.save', 'mysession'); 

包含您想要的会话参数

ini_set('session.use_trans_sid', 0);
ini_set('session.name', Configure::read('Session.cookie'));
ini_set('session.cookie_lifetime', $this->cookieLifeTime); 
ini_set('session.cookie_path', $this->path);

//overwrite Cake setting already set by CakeSession 
ini_set('session.cookie_secure', 0); 

在 app/config 中:创建一个 mysession.php 文件,其中 请注意,如果您将 cookie 设置为不安全,则使用 HTTPS 的大部分好处都会消失,因为由于 HTTPS HTTP 使用相同的会话 cookie,因此很容易窃取它,然后也可以窃取 HTTPS 的会话。

我遇到过一个 Web 应用程序,其中包含通过 HTTPS 的管理部分和通过 HTTP 的公共部分,也需要会话。我设法通过指定两个不同的 cookie 名称来分隔这两个部分:

在 app_controller.php 中:

if(env('HTTPS')
{
    Configure::write('Session.save', Configure::read('Session.save') . '_https');
}

在 app/config 中,创建另一个名为 *mysession_https.php* 的文件

ini_set('session.use_trans_sid', 0);
ini_set('session.name', Configure::read('Session.cookie') . 'S');

//cookie destroyed when users close their browser 
ini_set('session.cookie_lifetime', 0); 

ini_set('session.cookie_path', $this->path);

ini_set('session.cookie_secure', 1); 

这会创建两个不同的会话,一个通过 HTTP,一个通过 HTTPS,但在我的情况很好,因为所有敏感或私有数据都通过 HTTPS 传输,而通过 HTTP 传输的部分只需要会话即可获得更流畅的导航。

Modifying the core code is most of the time a bad thing and in your case, there is a possibility to do what you need, but the Cake way.

In app/config/core.php: modify the Session.save value

//Configure::write('Session.save', 'php');

//this will look for a 'mysession.php' file in app/config
Configure::write('Session.save', 'mysession'); 

In app/config: create a mysession.php file with the parameters you want for your session

ini_set('session.use_trans_sid', 0);
ini_set('session.name', Configure::read('Session.cookie'));
ini_set('session.cookie_lifetime', $this->cookieLifeTime); 
ini_set('session.cookie_path', $this->path);

//overwrite Cake setting already set by CakeSession 
ini_set('session.cookie_secure', 0); 

Also be aware that if you set your cookies to be not secure, most of the benefit of using HTTPS is gone, because as the same session cookie is used for HTTPS and HTTP, it becomes easy to steal it and then to steal the session for HTTPS as well.

I had the case of a webapp with an admin part over HTTPS and a public part over HTTP that required session as well. I manage to separate both parts by specifying two different cookie names:

In app_controller.php:

if(env('HTTPS')
{
    Configure::write('Session.save', Configure::read('Session.save') . '_https');
}

And in app/config, create another file called *mysession_https.php* with

ini_set('session.use_trans_sid', 0);
ini_set('session.name', Configure::read('Session.cookie') . 'S');

//cookie destroyed when users close their browser 
ini_set('session.cookie_lifetime', 0); 

ini_set('session.cookie_path', $this->path);

ini_set('session.cookie_secure', 1); 

This creates two different sessions, one over HTTP and one over HTTPS, but in my case it was fine, since all sensitive or private data are over HTTPS and the part over HTTP requires a session only to get a smoother navigation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文