php 在多个子域之间共享会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9153716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sharing SESSION Variables Between Multiple Subdomains
提问by Kevin Oluseun Karimu
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.
我有一个网站 www.example.com。这将有多个子域与单个应用程序或程序一起使用。例如,login.example.com 将允许用户登录站点,而 system.example.com 将允许用户访问信息系统,而 forums.example.com 将允许用户访问论坛。
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?
我们可能需要在子域之间传递信息,例如用户 ID 或用户偏好等。我们如何使用 SESSION 变量在子域之间传递信息?
EDIT: I like this idea:
编辑:我喜欢这个想法:
As the first thing in your script:
作为脚本中的第一件事:
ini_set('session.cookie_domain', '.example.com' );
回答by Shiplu Mokaddim
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.
PHP 会话 ID 保存在 Cookies 中。要使 cookie 在所有子域中可用,您需要将其分配给根域。然后所有子域将从 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 manualfor different approaches used to set an ini entry.
还要查看用于设置 ini 条目的不同方法的手册。
回答by Cheery
1) the subdomains should use the same path to save session files
1)子域应该使用相同的路径来保存会话文件
2) modify your
2)修改你的
php.ini session.cookie_domain = ".example.com"
配置文件 session.cookie_domain = ".example.com"
or .htaccess php_value session.cookie_domain .example.com
或 .htaccess php_value session.cookie_domain .example.com
or inside of the script ini_set('session.cookie_domain', '.example.com' );
或脚本内部 ini_set('session.cookie_domain', '.example.com' );
回答by Kevin Oluseun Karimu
I found a solution to my problem:
我找到了解决我的问题的方法:
session_name("2620368ghwahw90w");
session_set_cookie_params(0, '/', '.mydomain.com');
session_start();
This appears to work with no problem. Is this a good method with low security risk?
这似乎没有问题。这是一个安全风险低的好方法吗?
回答by Pathic
Before you create your session in php file, add this line at first line :
在 php 文件中创建会话之前,请在第一行添加以下行:
<?php
//session cross to sub domain
ini_set('session.cookie_domain', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
回答by Joseph
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)
你可以使用cookies。检查setcookie() 中的path
参数,该参数使该 cookie 可用于整个域。这样做的缺点是人们会关闭 cookie(私人浏览模式)
another method would be by passing the sessionID around using linksor hidden <input>
fields (for forms).
另一种方法是使用链接或隐藏<input>
字段(用于表单)传递 sessionID。
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.
由于单独的网站不共享会话(据我所知,因为子域在技术上是彼此“不同的地方”),所以不要使用会话来存储在服务器端。相反,使用数据库来处理您的会话。这样,多个站点可以共享同一个会话跟踪表。
回答by Philippe Gerber
To share the session cookie among subdomains, you have to set the cookie's domain to .example.org
(mind the dot).
要在子域之间共享会话 cookie,您必须将 cookie 的域设置为.example.org
(注意点)。
http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-domain
http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-domain
回答by Victor Okech
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();
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.
跨将使用会话变量的所有子域。我在我的索引 php 文件的开头设置了它,它可以工作。希望这会说清楚。
回答by DAVID AJAYI
Works like charm!
像魅力一样工作!
I believe the cleanest way is to create in you .env a variable SESSION_DOMAIN=.example.com
我相信最干净的方法是在你 .env 中创建一个变量 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
even example.com
shares same session
或者,您可以打开config/session.php
并设置'domain' => env('SESSION_DOMAIN', '.example.com')
所有子域,例如。domain.example.com
,test.example.com
甚至example.com
共享同一个会话