如何将 SESSION 变量传递给父目录中的页面? [英] How to pass SESSION variable to a page in the parent directory?

查看:34
本文介绍了如何将 SESSION 变量传递给父目录中的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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?

添加

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

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:

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

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'

在 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";

因此,a"和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?

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

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

之前

session_set_cookie_params.

推荐答案

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

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

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

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).

这篇关于如何将 SESSION 变量传递给父目录中的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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