在子域之间共享PHP中的会话变量 [英] Sharing session variables in PHP between subdomains

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

问题描述

我想使用许多子域在PHP中共享我的会话变量。我有一个例子:

I want to share my session variables in PHP using many subdomains. I have that example :


  • example.com

  • subdomain1.example.com

我尝试在两个域之间使用相同的会话变量。我已经在PHP中测试了很多功能,但没有任何效果。这是我的测试方法:

I try to use the same session variables between the two domains. I already tested a lot of functions in PHP but nothing work. Here is my way to test :

在example.com/page.php上,我进行了测试:

On the example.com/page.php, I have that test :

echo '<pre>';
var_dump(session_set_cookie_params(0, '/', '.example.com')); 
session_start(); 
echo "Session ID : ".session_id()."\n";
$_SESSION['foo'] = 'bar';
print_r($_SESSION);

在subdomain1.example.com/page.php上,我有一个:

And on subdomain1.example.com/page.php, I have that one :

echo '<pre>';
session_set_cookie_params(0, '/', '.example.com');
session_start();
echo "Session ID : ".session_id()."\n";
print_r($_SESSION);

我可以看到两个页面之间的会话ID相同,但是会话变量不可能在subdomain1.example.com/page.php

I can see that the session id is the same between the two pages, but session variables are impossible to read in subdomain1.example.com/page.php

我测试了许多功能,例如为会话设置名称,但没有更多结果。

I tested many functions, like set a name to the session, but with no more results.

谢谢。

推荐答案

我唯一想到的方法是将会话数据保存到cookie,然后在访问另一个域时打开cookie。您可以在此处阅读如何执行此操作: http://www.depiction。 net / tutorials / php / cookies-session-variables.php

The only way I can think of to do this would be to save the session data to a cookie, then open the cookie when the other domain is accessed. You can read how to do this here: http://www.depiction.net/tutorials/php/cookies-session-variables.php

出于好奇,为什么要这么做?

Out of curiosity, why do you want to do this?

这篇关于在子域之间共享PHP中的会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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