子域上的会话仍然无法正常工作 [英] Session on subdomains still not working

查看:38
本文介绍了子域上的会话仍然无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时前,我在这里创建了一个关于切换到 www 时不再存在的会话的线程.这应该解决了这个问题:

A few hours ago, I've created a thread here regarding a session which doesn't exist anymore when switching to www.. This should have fixed this:

session_set_cookie_params(0, '/', '.'.$_SERVER['HTTP_HOST']);

根据 php.net 和其他网站上的几个示例,这应该是正确的.在不使用 www 的情况下访问网站时.然后切换到 www.,会话仍然有效(我仍然记录它),但由于一些非常奇怪的原因,使用 www 时无法在网站上创建会话(因此登录).现在.

According to several examples on php.net and other websites, this should be correct. When going to the website without using www. and then switching to www., the session still works (I'm still logged it), but for some very strange reason, it's not possible to create the session (so to log in) on the website when using www. now.

这样我不用www的时候就可以登录了,我可以切换到www.所以会话仍然存在,但我在使用www时无法登录.

So I can login when not using www., I can switch to www. so the session still exists, but I can't login when using www.

谁能解释一下这种行为以及我如何解决这个问题?另外,使用该服务器变量安全吗?

Can anyone please explain this behaviour and how I can fix this? Also, is using that server variabele safe?

谢谢!

推荐答案

如果您从 www.example.com 请求某些资源,cookie 将设置为 .www.example.com,所以它只对 www.example.com 及其所有子域有效,但对 example.com 无效.

If you request some resource from www.example.com, the cookie will be set with .www.example.com, so it will only be valid for www.example.com and all its subdomains but not example.com.

在添加 . 之前删除前导 www.,例如:

Just remove the leading www. before prepending the ., for example:

$domain = '.' . preg_replace('/^www\./', '', strtolower($_SERVER['HTTP_HOST']));

这篇关于子域上的会话仍然无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆