2

I'm having some cookie troubles. They don't seem to be setting as they should be, and I haven't been able to figure out why. The url loads the urls details and then sets the cookie and then redirects them back to the main site. Here's the php code I am using

$cookie_file_path = 'cookie-path.txt';

// open a site with cookies
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.stuller.com/s/user');
curl_setopt($ch, CURLOPT_HEADER  ,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);   // Cookie management.
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
$content = curl_exec($ch);

// get cookies
$cookies = array();
preg_match_all('/Set-Cookie:(?<cookie>\s{0,}.*)$/im', $content, $cookies);

echo "<pre>";
print_r($cookies['cookie']); // show harvested cookies
echo "</pre>";
exit;

When I echo out the cookies this is what I see:

Array(
[0] =>  cart_session_id=365ad5b7-9ece-4255-aa68-3590d8841c17; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
[1] =>  CartRefresh=5/19/2014 2:05:46 PM; expires=Tue, 20-May-2014 07:05:46 GMT; path=/
[2] =>  ASP.NET_SessionId=yyloxijj2mke1ozxwg203mxh; path=/; HttpOnly
[3] =>  user_session_id=27c284ec-c5da-4c79-b860-f7bce9efac78; path=/
[4] =>  cms#51642=12719; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
[5] =>  cms#51772=152; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
[6] =>  cms#52083=13824; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
[7] =>  cms#52113=32; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
[8] =>  cms#52229=50; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
[9] =>  cms#52237=59; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
}

But in my cookies file it seems like everything is false and nothing is set:

www.stuller.com FALSE   /   FALSE   1432062346  cart_session_id 365ad5b7-9ece-4255-aa68-3590d8841c17
www.stuller.com FALSE   /   FALSE   1400569546  CartRefresh 5/19/2014 2:05:46 PM
#HttpOnly_www.stuller.com   FALSE   /   FALSE   0   ASP.NET_SessionId   yyloxijj2mke1ozxwg203mxh
www.stuller.com FALSE   /   FALSE   0   user_session_id 27c284ec-c5da-4c79-b860-f7bce9efac78
www.stuller.com FALSE   /   FALSE   1432062346  cms#51642   12719
www.stuller.com FALSE   /   FALSE   1432062346  cms#51772   152
www.stuller.com FALSE   /   FALSE   1432062346  cms#52083   13824
www.stuller.com FALSE   /   FALSE   1432062346  cms#52113   32
www.stuller.com FALSE   /   FALSE   1432062346  cms#52229   50
www.stuller.com FALSE   /   FALSE   1432062346  cms#52237   59

What am I doing wrong and why aren't they setting properly? And also, how can I read those cookies for the next curl method(s) as there will be about 4 or 5 hops that happen?

Thanks

MrTechie
  • 1,797
  • 4
  • 20
  • 36
  • Why do you think they're not set correctly? All the cookie names and values in the file match what you extracted from the header. – Barmar May 19 '14 at 21:23
  • 1
    As I explained in your other question http://stackoverflow.com/questions/23746403/cookies-php-curl-false?lq=1 the `FALSE` columns have nothing to do with whether the cookies are set, they're just boolean attributes of the cookie. – Barmar May 19 '14 at 21:25
  • I guess that's what confuses me. In the header they do not display the False/False but yet in the cookie file it does, and it makes me think the cookies is improperly set. Especially when I try to read the cookie again for the next curl and I get error messages on the server. – MrTechie May 19 '14 at 21:25
  • Ok so then when passing the cookie to the next curl attempt, I just use the CURLOPT_COOKIEFILE then correct? And should this curl even have COOKIEFILE associated with it? – MrTechie May 19 '14 at 21:28
  • Both those `FALSE` attributes are the default. The first would be `TRUE` if the cookie had `; Domain=.stuller.com`, the second would be `TRUE` if it contained `; Secure`. – Barmar May 19 '14 at 21:29
  • 1
    `CURLOPT_COOKIEJAR` is for incoming cookie updates, `CURLOPT_COOKIEFILE` is for outgoing cookies. Use both to persist cookies from one call to the next. – Barmar May 19 '14 at 21:32
  • Believe it or not - that actually helped me. I was calling FILE AFTER JAR when it should have been the other way around! :D – MrTechie May 19 '14 at 21:35

1 Answers1

3

When you want to send cookies, use the CURLOPT_COOKIEFILE option.

When you want to receive cookies and save them, use the CURLOPT_COOKIEJAR option.

If you want to send previously-saved cookies and also update the file with any changes the server makes, use both options.

Barmar
  • 741,623
  • 53
  • 500
  • 612