如何设置 CakePhp 的会话变量以与 Chrome 一起使用?

发布于 2024-12-10 02:23:30 字数 208 浏览 0 评论 0原文

我正在使用 CakePhp 开发一个网站。当上传到服务器并使用 Firefox 进行测试时,一切正常。

使用 Google Chrome 时,似乎没有创建会话变量。

如果我尝试访问主页,我会首先重定向到登录页面。登录后,它不会将我重定向到主页,而是再次重定向到登录页面。

我注意到会话变量不是用 Chrome 创建的,但在 Firefox 中工作得很好。

I am working on a website using CakePhp. When uploading to the server and testing with Firefox everything works great.

When using Google Chrome, it seems like no session variables are created.

If I try to access the home page, I'm redirected to the login page first. After I login it doesn't redirect me to the home page, but to the login page again.

What I noticed is that the session variables are not created with Chrome but works fine with Firefox.

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

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

发布评论

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

评论(3

素手挽清风 2024-12-17 02:23:30

查看此页面

这是链接

既然使用浏览器时应该改变服务器行为?好吧,我不太清楚 Chrome 对引用站点做了什么,但似乎它正在以某些方式改变它。
而且cakePHP强制将session.referer_check设置为true,从而检查具有相同PHPSESSID的多个请求是否来自同一个url。
正如 php.net 上的一篇文章所言:

如果您为 session.referer_check 指定了值,您可以运行
当有人访问您的网站并尝试登录时遇到困难
URL 大写错误。登录将失败,因为任何调用
session_start() 将导致现有会话被丢弃并且
一个新的正在创建。当登录时这会成为一个更大的问题
后面跟着 header("Location: ...") 重定向,因为
页面顶部的 session_start() 将失败。

这两种设置结合起来,你就陷入了混乱。我首先找到了一个快速修复方法,即在 app/webroot/index.php 中强制设置 session_start() 。但经过更多的阅读和调试,我终于找到了罪魁祸首。

破解修复方法

没有简单的方法可以阻止 cake 设置此设置,但您可以在 Session.save 配置键中定义自己的会话处理程序。
只需在 app/config/ 中创建名为 session_custom.php 的文件并设置Configure::write('Session.save', 'session_custom');在你的 core.php 文件中。
在该文件中,只需删除以下行(从 cake_session.php 复制/粘贴)

ini_set('session.referer_check', '');                    // Killing this f***ing config that was causing so much trouble with Chrome
ini_set('session.use_trans_sid', 0);                    // No session id in url
ini_set('session.name', Configure::read('Session.cookie'));    // Using custom cookie name instead of PHPSESSID
ini_set('session.cookie_lifetime', $this->cookieLifeTime);    // Cookie like time, depending on security level
ini_set('session.cookie_path', $this->path);                // Cookie path

Check this page

Here's the link.

Since when using a browser should change the server behavior ? Well I don't exactly know what Chrome is doing with the referer but it seems that it is altering it in some ways.
And cakePHP forces the setting of session.referer_check to true, thus checking that multiple requests with the same PHPSESSID comes from the same url.
As one posted on php.net :

If you have a value specified for session.referer_check you may run
into difficulty when someone accesses your site and attempts to log in
with a mis-capitalized URL. The logon will fail because any calls to
session_start() will result in the existing session being trashed and
a new one being created. This becomes a bigger problem when the logon
is followed by a header("Location: ...") redirect, because the
session_start() at the top of the page will fail.

Those two settings combined, and you got a hell of a mess. I first found a quick fix by forcing the setting of session_start() in app/webroot/index.php. But after more reading and debugging I finally found the culprit.

Hacking your way through the fix

There is no easy way to prevent cake from setting this setting, but you can define your own session handler in the Session.save configure key.
Just create file named session_custom.php in app/config/ and set Configure::write('Session.save', 'session_custom'); in your core.php file.
And in that file, just drop the following lines (copy/paste from cake_session.php)

ini_set('session.referer_check', '');                    // Killing this f***ing config that was causing so much trouble with Chrome
ini_set('session.use_trans_sid', 0);                    // No session id in url
ini_set('session.name', Configure::read('Session.cookie'));    // Using custom cookie name instead of PHPSESSID
ini_set('session.cookie_lifetime', $this->cookieLifeTime);    // Cookie like time, depending on security level
ini_set('session.cookie_path', $this->path);                // Cookie path
乞讨 2024-12-17 02:23:30

如果我是对的,那么您正在尝试通过从 url 检索获取值来设置会话
如果是这样,我建议你使用经典的 php 风格的 url,例如
http://localhost/something/projects/view?id=4f0998ad -0538-4454-b51a-0ca46f441e7f
并使用
$_GET['id'] 在你的控制器中检索值

我可以说,因为我前段时间遇到了同样的麻烦,并搜索了大量博客以获取合适的解决方案,但没有找到任何地方,但使用上述方法解决了我的问题

If i am right then you are trying to set the session by retrieving the get value from the url
if so the i suggest you use the classic php style url like
http://localhost/something/projects/view?id=4f0998ad-0538-4454-b51a-0ca46f441e7f
and use
$_GET['id'] in your controller to retrieve the value

I can say because i was facing the same trouble some time ago and searched through lots of blog to get an appropriate solution but got no where but using the above approach solved my p

伤痕我心 2024-12-17 02:23:30

在您的 app/Config/core.php

Configure::write('Session', array(
    'defaults' => 'cake',
        'timeout'  => 14400,  // 4 hours 
        'cookieTimeout' => 14400, // 4 hours 
        'cookie' => 'cookiename',
        )
    );

检查“cookie”中是否有空格,即使是下划线也可能会导致问题。

前段时间遇到了这个问题,我花了一段时间才弄清楚......

In your app/Config/core.php

Configure::write('Session', array(
    'defaults' => 'cake',
        'timeout'  => 14400,  // 4 hours 
        'cookieTimeout' => 14400, // 4 hours 
        'cookie' => 'cookiename',
        )
    );

check 'cookie' has no whitespaces in it, even underlines may cause a problem.

Had this problem some time ago and it took me a while to figure it out...

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