2

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.

Roman
  • 124,451
  • 167
  • 349
  • 456
  • If you are using cookies it appears to work fine. See http://stackoverflow.com/questions/644920/allow-php-sessions-to-carry-over-to-subdomains – GWW Feb 09 '11 at 17:35

2 Answers2

6

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

Cfreak
  • 19,191
  • 6
  • 49
  • 60
  • I thought it was possible to also work with sessions over an mysql connection? if so, multiple servers should be supported too. (it's just a guess, please tell if im mistaken) – xorinzor Nov 11 '11 at 19:48
  • @xorinzor the issue is the browser will only set the cookie for www.mysite.org by default. If you browse to somethingelse.mysite.org it doesn't matter that the session is still stored on the server because the browser won't pass the key from the cookie – Cfreak Nov 16 '11 at 03:21
0

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

Oliver
  • 347
  • 1
  • 4
  • 9
  • you're partially right about using .mysite.org but you can set it for the session as well. See my answer. – Cfreak Feb 09 '11 at 17:40
  • @cfreak - wow didn't know you could get away with this! thanks for the update. – Oliver Feb 09 '11 at 17:46