How to pass SESSION variable to a page in the parent directory?
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 开发者_Go百科"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
.
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).
You're right that sessions cannot be shared in this way. However, you can use secure cookies to share user data across subdomains:
http://us3.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
精彩评论