如何将SESSION变量传递给父目录中的页面?

发布于 2024-10-16 09:10:42 字数 1332 浏览 2 评论 0原文

在我的网站上,我希望有几个子域。为给定子域创建上下文的文件存储在相应的子目录中。

有时我需要链接到不属于子域的文件。

例如,在我的“subdomain1.mysite.org”上,我有一个指向“www.mysite.org/login.php”的链接。 “login.php”存储在包含与子域对应的所有子目录的目录中。

如果我以这种方式链接到“www.mysite.org/login.php”:href='../login.php',它不起作用。因为浏览器尝试访问“subdomain1.mysite.org/../login.php”。为了解决这个问题,我以这种方式创建链接: href='http://www.mysite.org/login.php' 但我认为通过这种方式我无法将会话变量传递给新页面(可以这样吗?)。

所以,我的问题是我找不到将会话变量传递到位于父目录中的页面(或位于另一个域上的页面)的方法。有谁知道这个问题如何解决?

添加

按照建议,我厌倦了使用session_set_cookie_params来解决问题。但我仍然无法得到想要的结果。更详细地说,我执行以下操作:

我在为 subdomain1.mysite.org 生成内容的 index.php 文件中使用以下代码:

session_set_cookie_params(24*60*60,'/','.mysite.org');
session_start();
$_SESSION['page'] = $PHP_SELF;

稍后,在同一个文件,我链接到我的其中一个页面(我认为问题可能出在这里)。我通过以下方式创建链接:

href='http://www.mysite.org/login.php'

在 login.php 文件中,我有以下代码:

session_set_cookie_params(24*60*60,'/','.mysite.org');
session_start();
print "a".$_SESSION['page']."b";

结果“a”和“b”之间没有任何内容。因此,我仍然无法将会话变量从一个页面传递到另一页面。有人知道我做错了什么吗?

添加2 我不得不说,如果我添加以下行,我的问题就解决了: session_name("some_name");

session_set_cookie_params之前

On my web site I would like to have several sub-domains. Files that create context for a given sub-domain are stored in corresponding sub-directory.

Sometimes I need to link to the files that do not belong to the sub-domain.

For example, on my "subdomain1.mysite.org" I have a link to "www.mysite.org/login.php". The "login.php" is stored in the directory that contains all subdirectories corresponding to the sub-domains.

If I make the link to the "www.mysite.org/login.php" in this way: href='../login.php', it does not work. Because browser tries to access "subdomain1.mysite.org/../login.php". To solve this problem I make the link in this way: href='http://www.mysite.org/login.php' but I think that in this way I cannot pass my session variables to the new page (can it be the case?).

So, my problem is that I cannot find a way to pass session variables to the page that is located in the parent directory (or to the page that is located on another domain). Does anybody know how this problem can be solved?

ADDED

As recommended, I tired to use session_set_cookie_params to solve the problem. But I still cannot manage to get the desired result. In more details I do the following:

I have in the index.php file that generates content for subdomain1.mysite.org I use the following code:

session_set_cookie_params(24*60*60,'/','.mysite.org');
session_start();
$_SESSION['page'] = $PHP_SELF;

Than later, in the same file I make a link to one of my pages (I think problem can be here). I create the link in the following way:

href='http://www.mysite.org/login.php'

In the login.php file I have the following code:

session_set_cookie_params(24*60*60,'/','.mysite.org');
session_start();
print "a".$_SESSION['page']."b";

As the result there is nothing between "a" and "b". So, I still cannot pass the session variables from one page to another one. Does anybody know what I am doing wrong?

ADDED 2
I have to say that my problem is solved if I add the following line:
session_name("some_name");

before the

session_set_cookie_params.

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

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

发布评论

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

评论(2

惟欲睡 2024-10-23 09:10:43

在 PHP INI 中,您可以将会话域设置为“.mysite.org”,然后您应该能够在多个域之间共享会话。相关变量是:

session.cookie_domain

您还可以使用 session_set_cookie_params() 函数在脚本中设置它。

编辑 - 我应该注意,只有当您的子域位于同一服务器上时(看起来像是),这才有效。

In PHP INI you can set the session domain to ".mysite.org" then you should be able to share the session between multiple domains. The relevant variable is:

session.cookie_domain

You can also set it inside your script with the session_set_cookie_params() function.

Edit - I should note this only works if your subdomains are on the same server (which looks like they are).

神也荒唐 2024-10-23 09:10:43

你是对的,会话不能以这种方式共享。但是,您可以使用安全 Cookie 跨子域共享用户数据:

https:// www.php.net/manual/en/function.setcookie.php

确保设置 cookie 时的域参数类似于

".mysite.org"

然后您将能够从 mysite.org 的任何子域访问 cookie

You're right that sessions cannot be shared in this way. However, you can use secure cookies to share user data across subdomains:

https://www.php.net/manual/en/function.setcookie.php

Make sure the domain parameter when you set the cookie is something like

".mysite.org"

Then you'll be able to access the cookie from any subdomain of mysite.org

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文