Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
758 views
in Technique[技术] by (71.8m points)

curl php setting cookies properly

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...