简体   繁体   English

在php中的子域上共享会话

[英]Share session on subdomains in php

I have a problem sharing the session between two subdomains, and I've read a lot of threads here and other places. 我在两个子域之间共享会话时遇到问题,并且在这里和其他地方已经阅读了很多线程。

I have www.xx.com and sub.xx.com and I've set 我有www.xx.com和sub.xx.com,并且已经设置

session_name("PHPSESSXX");
session_set_cookie_params(0, '/', '.xx.com');

and the session.save_path is the same on both domains. 并且session.save_path在两个域上都相同。

I get a cookie called PHPSESSXX on both domains, and it has the same value. 我在两个域上都得到一个名为PHPSESSXX的cookie,它具有相同的值。

When I log on to www.xx.com I get a session with some details in it, and it stays that way until I go to sub.xx.com. 当我登录www.xx.com时,我得到了一个包含一些详细信息的会话,并且一直保持这种状态,直到我转到sub.xx.com。 Then the session on sub.xx.com is empty, and if I refresh www.xx.com, the session there is gone as well. 那么sub.xx.com上的会话为空,如果我刷新www.xx.com,则该会话也消失了。 So it does something, but it seems to be overwriting the session data each time I visit a different subdomain. 这样就可以了,但是每次访问另一个子域时,它似乎都覆盖了会话数据。

Any ideas anyone? 有任何想法吗? - Can i debug this somehow? -我能以某种方式调试吗?

Btw: I'm using ssl on both domains. 顺便说一句:我在两个域上都使用ssl。

cheers 干杯

PHP session ids are saved in Cookies. PHP会话ID保存在Cookies中。 To make a cookie available in all the sub-domains you need to assign it to the root domain. 要使Cookie在所有子域中可用,您需要将其分配给根域。 Then all the sub-domains will get the session id from cookie and PHP can find the session using passed session id. 然后,所有子域都将从Cookie中获取会话ID,PHP可以使用传递的会话ID查找会话。

As it turns out, You just need to set the session.cookie_domain to the root domain in php.ini file 事实证明,您只需要将session.cookie_domain设置为php.ini文件中的根域

session.cookie_domain = ".example.com" Also check manual for different approaches used to set an ini entry. session.cookie_domain =“ .example.com”另请查看手册以了解用于设置ini条目的不同方法。

Your question is answered here 在这里回答您的问题

Sharing SESSION Variables Between Multiple Subdomains 在多个子域之间共享SESSION变量

My solution was to set a flag in .htaccess like this: 我的解决方案是在.htaccess设置一个标志,如下所示:

php_flag "suhosin.session.cryptdocroot" 0

And it now works perfectly ;o) 现在它可以完美运行; o)

The problem was that Suhosin was installed on the system, and the ini variable 问题是Suhosin已安装在系统上,并且ini变量

suhosin.session.cryptdocroot = On

encrypted the session files in such a way, that when a different subdomain tried to change the session, it deleted everything for security reasons. 加密会话文件的方式是,当另一个子域尝试更改会话时,出于安全原因,它将删除所有内容。

It didn't work for me to set the variable to Off or [nothing] in the ini-file, though maybe I didn't find the right file. 在ini文件中将变量设置为Off或[nothing]对我来说是行不通的,尽管也许我找不到正确的文件。

I also tried setting it in PHP without any luck. 我也尝试过在PHP中设置它,但没有任何运气。 Like this: 像这样:

ini_set('suhosin.session.cryptdocroot', 0)

cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM