Dentra Andres
Dentra Andres

Reputation: 381

Is it possible to send PHP session variables across domains through cURL?

If a person logs into a website (Website A) and this website creates session variables, could another website (Website B) make a cURL call to a PHP routine on Website A that would send back to Website B a json variable with Website A's session variables?

If so, how can I accomplish this?

This is the scenario I have in mind:

  1. User visits Website A and logs in. This create session variables in Website B
  2. In the same browser session, the user visits Website B.
  3. The homepage in Website B makes a cURL call to a specific routine in Website A that verifies if session variables exist and, if so, send their information back as JSon.
  4. Depending on the JSon variable content (if it exists), Website B shows the user a different content.

Is it doable through cURL?

Thanks.

Upvotes: 0

Views: 637

Answers (2)

Dentra Andres
Dentra Andres

Reputation: 381

As my websites are NOT in the same server, this is what I had to do to make it work (without memcache). This is not the most elegant solution but this is how I made it work, i.e., how to send to Website A the contents of the session variables of Website B:

  1. A php routine in Website A redirects using header() to Website B. The requesting php in Website A sends its URL as part of the query string so Website B knows where to send its answer.
  2. The receiving page in Website B grabs the session ID using session_id() and sends it back to Website A through an auto-submit post form.
  3. Now with the session ID from Website B, Website A makes a cURL call to Website B, which returns the content of the session variables of Website B.

Note: without the session ID, Website B sends an empty session variable to Website A.

Upvotes: 1

Andrew Fenn
Andrew Fenn

Reputation: 107

Haven't tried this so I have no idea if it would work however my best guess to get something similar to what you'd want is as follows.

Assuming both websites are on the same server you could manually set the session_id() and then keep track of this value. You then send this ID string to Website B and call session_id() there too with the same ID.

http://php.net/manual/en/function.session-id.php

If your Websites aren't on the same servers then you'd need to edit your php.ini and connect your session store to a service like memcache.

So for example.

Website A:

session_id('test');
$_SESSION['hello'] = "hello world\n";

Website B:

session_id('test');
echo $_SESSION['hello'];

Obviously you'd need to do a lot more work than above as you need to generate unique IDs for each user / request.

Upvotes: 2

Related Questions