从子域到主域的 PHP 会话变量 [英] PHP Session Variable from subdomain to main domain

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

问题描述

我在亚马逊 Web 服务器 EC2 实例上的 ISPConfig 中设置了 2 个域/站点,

i Have setup 2 domains/sites in the ISPConfig on amazon Web server EC2 instance,

我有一个名为 app.example.com 的域,另一个是 https://www.example.com两个域都在 ISpConfig3 中设置.所以两者都有单独的 webroot 和代码文件.我在 app.example.com 上设置了一些会话变量,而在 https://www.example.com 上我只有静态内容.但在某些情况下,我需要从 app.example.com 访问会话变量到我的顶级域 https://www.example.com

i have a domain named app.example.com and the other is https://www.example.com Both domains setup in ISpConfig3. so both have separate webroot and code files. I have setup some session variables on app.example.com , while on https://www.example.com i have only static contents. But in some cases i need to access the session variables from app.example.com to my top domain https://www.example.com

我尝试在 http://app.example.com

ini_set('session.cookie_domain', '.example.com' );

session_start();

$_SESSION['user_name']= "my_session";

然后在 https://www.example 上访问 $_SESSION['user_name'].com

and then accessing $_SESSION['user_name'] on https://www.example.com

print_r($_SESSION['user_name']);

但似乎没有任何效果,我怎样才能使它起作用?.

But nothing seems to be working, How can i make this to be working?.

推荐答案

@Alan Gularte 的第一条评论我认为回复得很好.如果它不起作用,您可以考虑自己编写一个cookie来来回传递信息.

The first comment of @Alan Gularte replies quite well I think. If it does not work, you may consider writing a cookie yourself to pass info back and forth.

setcookie ("PassMySessionName", $_SESSION['user_name'], time() - 3600, "/", "example.com");

将 cookie 域设置为example.com"将使其在 www.和应用程序.

Setting cookie domain to "example.com" will make it available both on www. and app.

另一方面,您会在 $_COOKIE 数组中找到它.

On the other side you will find it in the $_COOKIE array.

$PassedUserName=$_COOKIE["PassMySessionName"];

如果您需要在不启用 cookie 的情况下全部工作,它会变得更加复杂,因为 $_SESSION 将透明地将参数附加到您的 url.在这种情况下(如果您从一个站点链接到另一个站点),您可以手动将它们添加到网址中.

If you need it all to work without cookies enabled, it gets much more complicated, as $_SESSION will work appending parameters to your urls transparently. In that case (if you link from one site to the other) you may add them manually to the urls.

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

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