6

I have two site dev1.test.com and dev2.test.com.

These are two sites running on different servers. dev1.test.com is where I logged in and I have cookies set to *.test.com to validate if the user is logged.

Now on dev2.test.com, I want to check if the current user is logged-in by sending a PHP CURL request to dev1.test.com. In my curl request, I want to include the contents of $_COOKIE (where it has the cookie information of *.test.com) to this curl request.

How should I do that in php curl?

Mike Montesines
  • 151
  • 4
  • 8

4 Answers4

9

As you have a wildcard cookie domain, dev2 will also have the same cookies as dev1. So basically you need to say "pass my cookies to the other server through curl.

The curl option you want is "CURLOPT_COOKIE" and you pass in an string of the cookies "name1=value1;name2=value2"

Putting this together (untested - you need to wrap this amongst the other curl functions, of course)

$cookiesStringToPass = '';
foreach ($_COOKIE as $name=>$value) {
    if ($cookiesStringToPass) {
        $cookiesStringToPass  .= ';';
    }
    $cookiesStringToPass .= $name . '=' . addslashes($value);
}
curl_setopt ($ch, CURLOPT_COOKIE, $cookiesStringToPass );
Robbie
  • 17,605
  • 4
  • 35
  • 72
3

This is what I'm using to forward $_COOKIE via curl:

$cookie_data =
  implode(
    "; ",
    array_map(
      function($k, $v) {
        return "$k=$v";
      },
      array_keys($_COOKIE),
      array_values($_COOKIE)
    )
  );
curl_setopt($ch, CURLOPT_COOKIE, $cookie_data);

Reference: http://php.net/manual/en/function.curl-setopt.php

Tim Smith
  • 1,714
  • 2
  • 12
  • 14
2

Instead of working with $_COOKIE you could also use $_SERVER['HTTP_COOKIE'], which contains the HTTP header string.

I.e. you just need to write this:

curl_setopt($ch, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE']);
Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132
1

Read this: http://be2.php.net/manual/en/function.curl-setopt.php

CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR

So you have to read $_COOKIE from one server, save it to a file, and send it to another

It`s looks like:

curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132
Alexey Sidorov
  • 846
  • 1
  • 9
  • 17