3

I have a php proxy server to which email and password are posted on the beginning (login). I then use

$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookieJar);
curl_setopt( $ch, CURLOPT_HEADER, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

to post forward to some API that expects certain parameters. As you can see I save a cookie in a cookie jar file.

I can then use this cookie file to call any other requests to proxy -> API and successfully get the response. Everything works just fine. I use

curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookieJar);

to make other requests after user successfully signed in.

The problem is that only one user can login(and call other requests) at the time, because there is only one cookie jar file. I could probably generate unique cookie files on proxy and access them somehow with each new request by each user. But that is a load on the server and definitely not a good idea.

So what I would like to do is to save a cookie that is received into variable instead of a file and then send this to user...

This didn't work for me unfortunately; I can probably manage to write my own regex but I am wondering if there is a possibility to directly save a cookie into variable with curl or do I have to parse headers manually? (I want to be able to feed CURLOPT_COOKIEFILE with cookie in variable rathen than cookie in file)

Community
  • 1
  • 1
trainoasis
  • 6,419
  • 12
  • 51
  • 82

1 Answers1

4

Lets try this with a single curl handle($ch):

Making my first request:

$url= "http://www.google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, '-'); // <-- see here
$result = curl_exec($ch);

// remember i didn't close the curl yet!    

Now make another curl request with the same handle:

$url= "http://www.google.com";
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec($ch);
// if you are done, you can close it.
curl_close($ch);

In this example, I have used the - for the cookiejar. Which means it will not use any file. So during second curl request, it will use the cookiejar of previous call.

One problem: It will print the cookie jar values into std-output.

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • Thank you for your answer. Not creating a file is a good thing, but unfortunately it doesn't solve my problem-storing cookie into variable so I can send it and set it on client side – trainoasis Mar 03 '14 at 07:07