0

In the same browser, not on the same domain:

If on the site A, I store in session:

session_start();
$_SESSION['fruit'] = "Apple";

If on the site B, I store in session:

session_start();
$_SESSION['color'] = "green";

How can I echo on the site C :

// Apple is green.
session_start();
$_SESSION['fruit'].' is '.$_SESSION['color'];

Is it possible I echo, from another domain but in the same browser, all the $_SESSION variables ?

Thanks for the help.

3 Answers3

2

Short answer: No.

Long answer: Well... no but kind of yes, if you build two separate apps, one for each domain and send the session from one app to the other over an interface (e.g. a REST endpoint).

markus
  • 40,136
  • 23
  • 97
  • 142
  • Thanks. For security reason I was 99% sure about the no. I think I will be oblige to make a table for this step. Thanks. –  Oct 04 '15 at 17:15
1

Yes, you can.

You need to share your session ids around site A, site B and site C. Since session ids are usually stored in cookies, you need to transfer these information over all the domains.

If site A/B/C are all sub domain of the same site, then all you need to do is to change session.cookie_domain directive in your php.ini file like:

ini_set('session.cookie_domain', '.domain.com');

With this you can share session ids across multiple sub domains, like A.domain.com, B.domain.com, C.domain.com, ...

Harder is to do the same thing with different domain, like A.com, B.com, C.com, as you can't transfer over cookies because cookies are limited by the "same origin" policy. What you could do is to make cross-domain requests. You can find here some approach.

Now you need a common storage for the session data. Most websites use a database to store the session values instead of files, because databases are easier to scale in a multiple web server environment. You can use custom session handler to implement database storage. But if the domains are all on same server, all you need to do is to set the same session_save_path.

Community
  • 1
  • 1
Federkun
  • 36,084
  • 8
  • 78
  • 90
0

Session is limited to particular domain. So, you can not access in this way.

sandeepsure
  • 1,113
  • 1
  • 10
  • 17