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

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

问题描述

我有一个网站www.example.com.那将有多个与单个应用程序或程序一起工作的子域.例如,login.example.com将允许用户登录到站点,而system.example.com将允许用户访问信息系统,而forums.example.com将允许用户访问论坛.

I have a website www.example.com. That will have multiple subdomains that work with a single application or program. For an example, login.example.com will allow the user to log in to the site while system.example.com will allow the user to access an information system, while forums.example.com will allow the user to access forums.

我们可能需要在子域之间传递信息,例如用户ID或用户首选项等.如何使用SESSION变量在sudomain之间传递信息?

We may need to pass information between the subdomains such as a user id, or a user preference, etc. How do we go about passing information between the sudomains using SESSION variables?

我喜欢这个主意:

作为脚本中的第一件事:

As the first thing in your script:

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

推荐答案

PHP会话ID保存在Cookies中. 要使Cookie在所有子域中可用,您需要将其分配给根域.然后,所有子域都将从Cookie中获取会话ID,PHP可以使用传递的会话ID找到会话.

PHP session ids are saved in Cookies. To make a cookie available in all the sub-domains you need to assign it to the root domain. Then all the sub-domains will get the session id from cookie and PHP can find the session using passed session id.

事实证明,您只需要将session.cookie_domain设置为php.ini文件中的根域

As it turns out, You just need to set the session.cookie_domain to the root domain in php.ini file

session.cookie_domain = ".example.com"

还要检查手册用于设置ini条目的不同方法.

Also check manual for different approaches used to set an ini entry.

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

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