dsjz1119
2013-11-28 08:43
浏览 389
已采纳

curl_setopt使用CURLOPT_COOKIESESSION,CURLOPT_COOKIEJAR,CURLOPT_COOKIEFILE返回false

PHP version 5.4.19 under FreeBSD

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //returns true
curl_setopt($ch, CURLOPT_HEADER, true); //returns true
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6'); //returns true
curl_setopt($ch, CURLOPT_TIMEOUT, 100); //returns true
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //returns true
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //returns true
var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false) ); //true
var_dump( curl_setopt($ch, CURLOPT_COOKIESESSION, 1) ); //returns false ???? 
var_dump( curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie') ); //returns false ????
var_dump( curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie') ); //returns false ????
var_dump( curl_setopt($ch, CURLINFO_HEADER_OUT, true) ); //true

I've also noticed, that even without CURLOPT_COOKIESESSION, CURLOPT_COOKIEJAR, CURLOPT_COOKIEFILE - Curl is not passing any cookie between requests in single handler. Here is some headers log to understand what I'm talking about:

GET /login.php HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6
Host: domain.com
Accept: */*

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Thu, 28 Nov 2013 08:25:57 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: PHPSESSID=bev3mvr9s27jgq5vvm43s56pb2; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: go.php?PHPSESSID=bev3mvr9s27jgq5vvm43s56pb2

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Thu, 28 Nov 2013 08:25:57 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: PHPSESSID=m8uddnk1q55s634fu7frnt2lo3; path=/ 
    #session cookie has changed; Curl didn't use PHPSESSID cookie from login.php
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: login.php

UPDATE: I've tried the same code on another host - everything is ok. Hm...

图片转代码服务由CSDN问答提供 功能建议

FreeBSD下的PHP版本5.4.19

  $ ch =  curl_init(); 
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);  //返回true 
curl_setopt($ ch,CURLOPT_HEADER,true);  //返回true 
curl_setopt($ ch,CURLOPT_USERAGENT,'Mozilla / 5.0(Windows; U; Windows NT 5.1; en-US; rv:1.9.2)Gecko / 20100115 Firefox / 3.6');  //返回true 
curl_setopt($ ch,CURLOPT_TIMEOUT,100);  //返回true 
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true);  //返回true 
curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,false);  //返回true 
var_dump(curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false));  // true 
var_dump(curl_setopt($ ch,CURLOPT_COOKIESESSION,1));  //返回false ????  
var_dump(curl_setopt($ ch,CURLOPT_COOKIEJAR,'/ tmp / cookie'));  //返回false ???? 
var_dump(curl_setopt($ ch,CURLOPT_COOKIEFILE,'/ tmp / cookie'));  //返回false ???? 
var_dump(curl_setopt($ ch,CURLINFO_HEADER_OUT,true));  // true 
   
 
 

我也注意到,即使没有CURLOPT_COOKIESESSION,CURLOPT_COOKIEJAR,CURLOPT_COOKIEFILE - Curl也没有在单个处理程序中的请求之间传递任何cookie。 Here is 一些头文件记录以了解我在说什么:

  GET /login.php HTTP / 1.1 
User-Agent:Mozilla / 5.0(Windows; U; Windows NT 5.1  ; en-US; rv:1.9.2)Gecko / 20100115 Firefox / 3.6 
主机:domain.com 
接受:* / * 
 
HTTP / 1.1 302暂时移动
服务器:nginx 
日期:星期四,2013年11月28日 08:25:57 GMT 
内容类型:text / html 
Transfer-Encoding:chunked 
Connection:keep-alive 
Set-Cookie:PHPSESSID = bev3mvr9s27jgq5vvm43s56pb2;  path = / 
Expires:Thu,19 Nov 1981:08:52:00 GMT 
Cache-Control:no-store,no-cache,must-revalidate,post-check = 0,pre-check = 0 
Pragma:no-  cache 
Location:go.php?PHPSESSID = bev3mvr9s27jgq5vvm43s56pb2 
 
HTTP / 1.1 302暂时移动
服务器:nginx 
日期:星期四,28十一月2013 08:25:57 GMT 
内容类型:text / html;  charset = utf-8 
Transfer-Encoding:chunked 
Connection:keep-alive 
Set-Cookie:PHPSESSID = m8uddnk1q55s634fu7frnt2lo3;  path = / 
 #session cookie已更改;  Curl没有使用来自login.php的PHPSESSID cookie 
Expires:Thu,1981年11月19日08:52:00 GMT 
Cache-Control:no-store,no-cache,must-revalidate,post-check = 0,pre-pre  check = 0 
Pragma:no-cache 
Location:login.php 
   
 
 

UPDATE:我在另一个上尝试了相同的代码 主持人 - 一切都好。 嗯......

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsfsfsfsfs55656 2013-11-28 09:29
    已采纳

    I don't know why, but Curl was compiled without cookies support. Just reinstalled it with --enable-cookies - that's it.

    点赞 打赏 评论

相关推荐 更多相似问题