Sending cookies stored globally $ _COOKIE using PHP curl

I have two sites 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 check if the user is registered.

Now, at dev2.test.com, I want to check if the current user is registered 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 cookie information * .test.com) in this curl request.

How do i do this in php curl?

+6
source share
4 answers

Since you have a wildcard cookie, dev2 will also have the same cookies as dev1. So basically you should say: "Transfer my cookies to another server via curl.

The curl parameter you want is "CURLOPT_COOKIE" and you pass the cookie string "name1 = value1; name2 = value2"

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

$cookiesStringToPass = ''; foreach ($_COOKIE as $name=>$value) { if ($cookiesStringToPass) { $cookiesStringToPass .= ';'; } $cookiesStringToPass .= $name . '=' . addslashes($value); } curl_setopt ($ch, CURLOPT_COOKIE, $cookiesStringToPass ); 
+9
source

This is what I use to send $ _COOKIE through 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); 

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

+3
source

Read the following: 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 looks like this:

 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt"); curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt"); 
+1
source

Instead of working with $_COOKIE you can also use $_SERVER['HTTP_COOKIE'] , which contains an HTTP header line.

those. you just need to write this:

 curl_setopt($ch, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE']); 
+1
source

All Articles