PHP: passing session variables from one domain to another domain

Asked

Viewed 573 times

1

We have two projects hosted in different domains. I need to pass some information from one domain to the other, to generate a ticket.

I was asked to send session variables to this other domain for billet generation.

After some searches, I saw that it is not possible to pass session data from one domain to another, there is some solution to circumvent this?

The rules for me to recover the desired information in another domain are as follows::

  1. Set the session variables;
  2. Then I redirect to the page of the other domain, where it already takes these variables;
  3. I carry the information I need.
  • These "projects" are in PHP?

  • are yes, all two in php, but in different frameworks only.

  • So just use include or require/require_once, already tried?

  • how so? use require? but the projects are of different domains

  • 2

    Possible duplicate of Put site path in a PHP include

  • not at all. It’s crazy?

  • Have you tried passing on the relevant information via POST or GET? What you want to do is very similar to a web service query

  • @Do these two projects run on the same server? If yes and you have control over this server, you may be able to share sessions. Read that. However, I find it more practical and guaranteed to do this communication through a simple HTTP request. Just one application does the POST request, as Jefferson commented, for the other application by sending the required data and treating the response.

  • yes, they are on the same server.

Show 4 more comments

1 answer

1

In fact, it is impossible to pass session variables from one domain to another directly. Which doesn’t mean there aren’t other ways to solve your problem.

If you have two different domains running applications, even with the same source code, for all intents and purposes you have two isolated systems. You have two ways to get them to communicate:

  • Through a database of. If both applications have access to the same database, you can write to the database tables with one application and read with the other. The logic of how this would be done is up to you, but here is a suggestion: write the session variables of the first application in the database and trigger a request to a URI of the second application that makes it read this data. I recommend deleting the data after use.

  • Through service. This is usually the most elegant form. The application that needs to receive session variables can expose a service. The application that generates session variables can consume this service. This has the advantage of using the most standard form of communication between applications and keeping the databases of both systems isolated.

For the second case, the flow is as follows:

  1. First application generates set of values that currently you guard in session;
  2. First application consumes service of second, passing these values and user identification;
  3. The second application stores these values (in a bank or in a cache), always associating a set of values to a user;
  4. First application uses Javascript to open a new window/tab of the second application;
  5. User logs in the second application;
  6. Second application recovers user data that are in the database or in the cache and does what you have to do with it (in your case, generate a billet).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.