在多个子域之间共享 SESSION 变量

发布于 2025-01-02 21:38:07 字数 331 浏览 6 评论 0 原文

我有一个网站 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' ); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

月竹挽风 2025-01-09 21:38:07

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' );

南薇 2025-01-09 21:38:07

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

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

session.cookie_domain = ".example.com"

同时检查 手册 用于设置 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 in php.ini file

session.cookie_domain = ".example.com"

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

凤舞天涯 2025-01-09 21:38:07

我找到了解决问题的方法:

session_name("2620368ghwahw90w");
session_set_cookie_params(0, '/', '.mydomain.com');
session_start();

这似乎没有问题。这是一个安全风险低的好方法吗?

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?

饮惑 2025-01-09 21:38:07

在 php 文件中创建会话之前,请在第一行添加以下行:

<?php
//session cross to sub domain
ini_set('session.cookie_domain', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));

Before you create your session in php file, add this line at first line :

<?php
//session cross to sub domain
ini_set('session.cookie_domain', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
以酷 2025-01-09 21:38:07

你可以使用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.

鹿港小镇 2025-01-09 21:38:07

要在子域之间共享会话 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

始于初秋 2025-01-09 21:38:07

我已经研究这个问题有一段时间了,对我有用的是将代码放在下面:

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.

望她远 2025-01-09 21:38:07

效果就像魅力!

我相信最干净的方法是在 .env 中创建一个变量 SESSION_DOMAIN=.example.com

或者,您可以打开 config/session.php 并设置 '域' => env('SESSION_DOMAIN', '.example.com'),
所有子域例如。 domain.example.comtest.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 even example.com shares same session

浊酒尽余欢 2025-01-09 21:38:07

这应该适用于大多数(如果不是全部)情况:

<?php

if (!session_id()) /* If session is not started yet, then... */
    {
        ini_set('session.cookie_domain', substr_count($_SERVER['SERVER_NAME'],'.') > 1 ? ('.'.substr($_SERVER['SERVER_NAME'], strpos($_SERVER['SERVER_NAME'], '.') + 1)) : ('.'.$_SERVER['SERVER_NAME']));
        session_start(); /* Start session now. */
    };
    
?>

This should work in most, if not all, cases:

<?php

if (!session_id()) /* If session is not started yet, then... */
    {
        ini_set('session.cookie_domain', substr_count($_SERVER['SERVER_NAME'],'.') > 1 ? ('.'.substr($_SERVER['SERVER_NAME'], strpos($_SERVER['SERVER_NAME'], '.') + 1)) : ('.'.$_SERVER['SERVER_NAME']));
        session_start(); /* Start session now. */
    };
    
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文