我有一个网站 www.example.com。这将具有与单个应用程序或程序一起使用的多个子域。例如,login.example.com将允许用户登录站点,而system.example.com将允许用户访问信息系统,而forums.example.com将允许用户访问论坛。
我们可能需要在子域之间传递信息,例如用户 ID 或用户首选项等。我们如何使用 SESSION 变量在子域之间传递信息?
编辑:
我喜欢这个想法:
作为脚本中的第一件事:
ini_set('session.cookie_domain', '.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.
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?
EDIT:
I like this idea:
As the first thing in your script:
ini_set('session.cookie_domain', '.example.com' );
发布评论
评论(9)
1) 子域名应使用相同的路径来保存会话文件
2) 修改
php.ini
session.cookie_domain = ".example.com"
或 .htaccess
php_value session.cookie_domain .example。 com
或脚本内部
ini_set('session.cookie_domain', '.example.com' );
1) the subdomains should use the same path to save session files
2) modify your
php.ini
session.cookie_domain = ".example.com"
or .htaccess
php_value session.cookie_domain .example.com
or inside of the script
ini_set('session.cookie_domain', '.example.com' );
PHP 会话 ID 保存在 Cookie 中。 要使 Cookie 在所有子域中可用,您需要将其分配给根域。然后所有子域将从cookie中获取会话id,并且PHP可以使用传递的会话id找到会话。
事实证明,您只需将
session.cookie_domain
设置为php.ini
文件中的根域,同时检查 手册 用于设置 ini 条目的不同方法。
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.
As it turns out, You just need to set the
session.cookie_domain
to the root domain inphp.ini
fileAlso check manual for different approaches used to set an ini entry.
我找到了解决问题的方法:
这似乎没有问题。这是一个安全风险低的好方法吗?
I found a solution to my problem:
This appears to work with no problem. Is this a good method with low security risk?
在 php 文件中创建会话之前,请在第一行添加以下行:
Before you create your session in php file, add this line at first line :
你可以使用cookies。检查 setcookie() 中的
path
参数,这使得该 cookie 可用于整个域。这样做的缺点是人们会关闭 cookie(隐私浏览模式),另一种方法是 传递使用链接或隐藏
字段(对于表单)的sessionID。
由于单独的网站不共享会话(据我所知,因为子域在技术上是彼此“不同的地方”),所以不要使用会话存储在服务器端。相反,使用数据库来处理您的会话。这样,多个站点可以共享同一个会话跟踪表。
you can use cookies. check the
path
parameter in setcookie() which makes that cookie available for he entire domain. drawbacks to this are people who turn off cookies (private browsing modes)another method would be by passing the sessionID around using links or hidden
<input>
fields (for forms).since separate websites don't share sessions (as far as i know, since subdomains are technically "different places" from eachother), don't use sessions to store on the server side. instead, use a database to handle your sessions. that way, multiple sites can share the same session tracking table.
要在子域之间共享会话 cookie,您必须将 cookie 的域设置为
.example.org
(注意点)。http://www.php.net/manual /en/session.configuration.php#ini.session.cookie-domain
To share the session cookie among subdomains, you have to set the cookie's domain to
.example.org
(mind the dot).http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-domain
我已经研究这个问题有一段时间了,对我有用的是将代码放在下面:
session_name("some_session_name");
session_set_cookie_params(0, '/', '.some_domain.com');
session_start();
跨所有将使用会话变量的子域。我将其设置在我的索引 php 文件的开头并且它有效。希望这能澄清这一点。
I have been going round with this for a while now and what worked for me is placing the code below:
session_name("some_session_name");
session_set_cookie_params(0, '/', '.some_domain.com');
session_start();
across all the sub-domains that will use the session variables. I set this at the beginning of my index php file and it works. Hope this will make it clear.
效果就像魅力!
我相信最干净的方法是在 .env 中创建一个变量
SESSION_DOMAIN=.example.com
或者,您可以打开
config/session.php
并设置'域' => env('SESSION_DOMAIN', '.example.com')
,所有子域例如。
domain.example.com
、test.example.com
甚至example.com
共享同一会话Works like charm!
I believe the cleanest way is to create in you .env a variable
SESSION_DOMAIN=.example.com
Alternatively, you can open up
config/session.php
and set'domain' => env('SESSION_DOMAIN', '.example.com')
,with that all subdomains eg.
domain.example.com
,test.example.com
evenexample.com
shares same session这应该适用于大多数(如果不是全部)情况:
This should work in most, if not all, cases: