5

Im really new to the php curl concept, can anybody show me a simple example of how to set a cookie in the browser using php curl

this is my code that doesnt work...

$ch = curl_init('http://localhost/setc.php?userid=123&panelid=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// get headers too with this line
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_exec($ch);
curl_close($ch);

but when i take a var_dump of this it gets printed, very confused please help

array (size=1)
  'userid' => string '1:123' (length=8)

please if ur confused....u can simply ignore my above coding and let me know how to set a simple cookie using php curl

Sho Gum Lew
  • 329
  • 1
  • 5
  • 17

3 Answers3

21

I'm not clear on what you are expecting to have happen with cookies. However, you might try this:

$ch = curl_init('http://localhost/setc.php?userid=123&panelid=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// get headers too with this line
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIE, 'cookiename=cookievalue');
curl_exec($ch);
curl_close($ch);
gview
  • 14,876
  • 3
  • 46
  • 51
6

Php Curl allows you to directly set the cookie Header, using:

curl_setopt($curlHandle,CURLOPT_COOKIE, $cookieString);

Where $cookieString holds semicolon separated key=value pairs of cookie data.

Please refer to this question: PHP Curl and setcookie problem

And be sure to thoroughly go through http://php.net/manual/en/function.curl-setopt.php

Community
  • 1
  • 1
Rohan
  • 139
  • 1
  • 5
5

Try this:

curl_setopt($ch , CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch , CURLOPT_COOKIEFILE, 'cookies.txt');

This will automatically create one cookies.txt

Yash
  • 1,446
  • 1
  • 13
  • 26