会话和子域 [英] Sessions and subdomains

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

问题描述

我一直在尝试让我的会话在我的子域中运行,我很确定我在星期一开始工作,但是在星期二添加了一些代码后,它在星期三不起作用!我使用了代码 ini_set("session.cookie_domain", $domain); where $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);,然后开始会话.接下来我检查会话是否已过期.如果会话已过期,则当前会话将被销毁并取消设置,然后创建一个新会话.剩下的就是设置用户信息.

我最近添加的唯一内容是会话到期检查器.我试过绕过它,但它没有改变任何东西.

非常感谢任何帮助.如果方便的话,我可以发布代码.

麦克

解决方案

请添加一些代码 :).

我只能告诉您我们是如何实现相同功能的.尝试添加

<目录/path/to/your/docroot">php_value session.cookie_domain ".example.com"</目录>

到您的虚拟主机配置.这是我们唯一需要做的事情才能使这个功能发挥作用.现在我们可以使用相同的 cookie 访问所有子域,而无需添加所有额外的代码.我不是说这是一个解决方案,但这种方法使测试变得不那么复杂.

编辑

您可以在网络服务器的配置中设置虚拟主机.假设您使用 apache,它们将出现在 httpd.conf 中或出现在文件系统上的其他文件中,这些文件包含在您的 httpd.conf 中.httpd.conf 在您系统上的位置取决于您的配置,但如果您使用 Linux,它可能位于/etc/apache、/etc/httpd、/usr/local/apache、/usr/local/httpd 中的某个位置

一旦你找到这个文件,它就会有一个或多个这样的条目:

ServerAdmin webmaster@yourdomain.orgDocumentRoot/var/www/yourdomain/www服务器名称 yourdomain.org<目录/var/www/yourdomain/www">选项 FollowSymLinks 包括允许覆盖所有命令允许,拒绝所有人都允许</目录></虚拟主机>

并修改如下代码:

ServerAdmin webmaster@yourdomain.orgDocumentRoot/var/www/yourdomain/www服务器名称 yourdomain.org<目录/var/www/yourdomain/www">选项 FollowSymLinks 包括允许覆盖所有命令允许,拒绝所有人都允许php_value session.cookie_domain ".yourdomain.org"</目录></虚拟主机>

注意 php_value session.cookie_domain ".yourdomain.org" 行.

将此行添加到此域的所有服务器配置中,您的 cookie 将被共享.

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

解决方案

Please add some code :).

I can only tell you how we achieved the same functionality. Try adding

<directory "/path/to/your/docroot">
    php_value session.cookie_domain ".example.com"
</directory>

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:

<VirtualHost *:80>
    ServerAdmin webmaster@yourdomain.org
    DocumentRoot /var/www/yourdomain/www
    ServerName yourdomain.org
    <directory "/var/www/yourdomain/www">
                Options FollowSymLinks Includes
                AllowOverride All
                Order allow,deny
                Allow from all
        </directory>
</VirtualHost>

And modify the code that it looks like this:

<VirtualHost *:80>
    ServerAdmin webmaster@yourdomain.org
    DocumentRoot /var/www/yourdomain/www
    ServerName yourdomain.org
    <directory "/var/www/yourdomain/www">
                Options FollowSymLinks Includes
                AllowOverride All
                Order allow,deny
                Allow from all
                php_value session.cookie_domain ".yourdomain.org"
        </directory>
</VirtualHost>

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.

这篇关于会话和子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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