会话和子域
我一直在尝试让我的会话在我的子域上运行,我很确定我在周一可以工作,但在周二添加一些代码后,周三就无法工作了!我使用了代码 ini_set("session.cookie_domain", $domain);
,其中 $domain = .example.com
。
我网站的主页当前位于 test.example.com,我通过 test.example.com/login
访问登录页面。当我输入这个地址时,地址栏中的url会自动更改为http://www.test.example.com/login
,这就是问题所在。该会话是为 www.test.example.com
创建的,但网站上的大多数链接都指向 test.example.com/
。
我唯一能想到的可能是我处理会话的方式。在每个页面中都会启动一个会话。首先设置 ini_set("session.cookie_domain", $domain);
,然后启动会话。接下来我检查会话是否已过期。如果会话已过期,则当前会话将被销毁并取消设置,然后将创建一个新会话。剩下的就是设置用户信息了。
我最近添加的唯一内容是会话过期检查器。我尝试过绕过它,但它没有改变任何东西。
非常感谢任何帮助。如果它更容易的话我可以发布代码。
麦克风
I've been trying to get my sessions running across my subdomains, which I'm pretty sure I got working on Monday but after adding some code Tuesday its not working Wednesday! I've used the code ini_set("session.cookie_domain", $domain);
where $domain = .example.com
.
My site's main page is currently located on test.example.com and I access the login page through test.example.com/login
. When i enter this address, the url in the address bar is automatically changed to http://www.test.example.com/login
, and this is where the problem lies. The session is created for www.test.example.com
but most links on the site direct to test.example.com/<sub folder>
.
The only thing I can think of that might be throwing it off is the way I handle sessions. In every page a session is started. First the ini_set("session.cookie_domain", $domain);
is set, then the session is started. Next I check to see if the session has expired. If the session has expired the current session is destroyed and unset then a new session is created. The rest is just setting up user information.
The only thing I've added recently is the session expiry checker. I've tried bypassing it but it hasn't changed anything.
Any help is greatly appreciated. I can post code if it makes it easier.
Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请添加一些代码:)。
我只能告诉你我们如何实现相同的功能。尝试添加
到您的虚拟主机配置。这是我们必须做的唯一事情才能让这个功能发挥作用。现在我们可以使用相同的 cookie 访问所有子域,而无需添加所有额外的代码。我并不是说这是一个解决方案,但这种方法使测试变得不那么复杂。
编辑
您可以在网络服务器的配置中设置虚拟主机。假设您使用 apache,它们将位于 httpd.conf 中,或者存在于文件系统上包含在 httpd.conf 中的其他文件中。 httpd.conf 在您系统上的位置取决于您的配置,但如果您使用 Linux,它可能位于 /etc/apache、/etc/httpd、/usr/local/apache、/usr/local/httpd 中的某个
位置找到该文件后,它将包含一个或多个如下所示的条目:
并修改代码,使其看起来像这样:
注意
php_value session.cookie_domain ".yourdomain.org"
行。将此行添加到该域的所有服务器配置中,您的 cookie 将被共享。
Please add some code :).
I can only tell you how we achieved the same functionality. Try adding
to your virtual host configs. This was the only thing we had to do to make this functionality work. Now we can access all subdomains with the same cookies without adding all the extra code. I don't say this is a solutions, but this approach makes testing a lot less complicated.
Edit
You can set virtual hosts in the configuration of your webserver. Assuming you use apache they will be either in httpd.conf or are present in other files on the filesystem which are included in your httpd.conf. Where httpd.conf is located on your system depends on your configuration, but if you use Linux it will probably be somewhere in /etc/apache, /etc/httpd, /usr/local/apache, /usr/local/httpd
Once you have located this file it will have one or more entries like this:
And modify the code that it looks like this:
Notice the
php_value session.cookie_domain ".yourdomain.org"
line.Add this line to all server configuration for this domain and your cookies will be shared.
如果不了解更多细节,这是不可能调试的。
您可能需要首先检查 cookie 是否设置正确,以及它们是否实际返回到服务器。
使用可让您在浏览器上查看标头的工具(Firefox 的 webdeveloper 工具栏/liveheaders/firebug),并查看服务器是否确实请求浏览器接受 cookie - 以及请求的目的。
This is impossible to debug without knowing more details.
You might want to first check if the cookies are being set properly, and if they are actually being returned to the server.
Use a tool which lets you see headers on your browser (webdeveloper toolbar / liveheaders / firebug for Firefox) and see if the server is actually requesting that the browser accept a cookie - and for what.
请原谅我不知道“虚拟主机配置”是什么。我的代码运行如下:
主页将包含
session.php
会话文件包含另一个名为
serverFunctions
的 PHP 文件。这只是一个允许我格式化 URL 等的类。当用户登录时,登录请求由 process_request.php 处理。
如果我遗漏了任何内容或需要进一步解释发生的情况,请告诉我。
Forgive me for not knowing but what 'virtual host configs' is. My code runs something like this:
The main page will include
session.php
The session file includes another PHP file called
serverFunctions
. This is just a class that allows me to format URL and such.When the user logs in, the login request is handled by
process_request.php
If I'm missing anything or need to explain what happens a bit more let me know.