Search code examples
phpregexcookiescurlsetcookie

Copying Cookie From Another Domain To My Domain


I'm making a cURL request from exampleA.com to get its response header. So far so good, these are some of the data I get:

Array
(
    [0] =>  sess=1; path=/; expires=Wed, 15-May-2013 09:25:29 GMT; domain=.exampleA.com; HttpOnly
    [1] =>  .........
)

Now this is the hard part for me, I want to set the cookie to my own domain exampleB.com, the same as how I got it from exampleA.com.

Using Firebug this is the Response Header in exampleA.com:

Set-Cookie:uuid2=4511997856767122744; path=/; expires=Mon, 12-Aug-2013 09:21:38 GMT; domain=.exampleA.com; HttpOnly

So I need to set the cookie to the same values, but under the domain of exampleB.com. How do I do that?


Solution

  • $ch = curl_init('http://www.google.com/');
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);
    
    preg_match('/Set-Cookie:[^\r\n]+/', $response, $match); // extract cookie header
    $cookie_header = preg_replace('/domain=[^;\r\n]+/', 'domain=.mydomain.com', $match[0]); // replace old domain with your domain
    
    //echo $cookie_header;
    header($cookie_header); // set cookie header