Page 1 of 1

cURL and COOKIES -How to set?

Posted: Fri Nov 21, 2003 10:33 pm
by alexeiz
I'm trying to set cookies when posting to a page.

I got the script practically copied from PHP Cookbook with minor alterations:

$post_arr = array (username =>'alexeiz' ,password =>'GOVNO32');
$c = curl_init('http://www.alexeiz.com/hit.php');
curl_setopt($c,CURLOPT_VERBOSE,1);
curl_setopt($c,CURLOPT_COOKIE,'user=MUDAK');
curl_setopt($c, CURLOPT_POSTFIELDS, $post_arr);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($c);
curl_close($c);

echo "<br> $page";
Postfields are posted OK, but the cookie is never set - tried many times in different enviroments.

What could be the problem? PHP is 4.3.2, cURL 7.10.3

Posted: Sat Nov 22, 2003 11:56 am
by Stoker
Perhaps you are missunderstanding the http protocol a bit, you can not "set" a cookie at a webserver, curl is a client so your script is the one holding a cookie, when you set the curl cookie options it is to give that cookie value to the webserver...

I do misunderstand!

Posted: Sat Nov 22, 2003 12:22 pm
by alexei
Thanks, I do misunderstand I guess!

Let me explain what my end goal is - I'm posting to a login page that requires username and password.

When I login to this page manually, I also set cookies holding username and password so I could access other protected pages.

I was told by about 5-6 different sources that what I have to do to simulate manual login is to extract the info on these cookies from the header returned and then submit them back in the next cURL submission to the same page using either CURLOPT_COOKIE or CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE

I tried all possible combinations using numerous examples and never succeeded. The example I posted is stripped down and tries to set a different cookie on purpose.

I would appreciate any suggestion that would lead to successful login AND setting of username and password cookies from the script.