【问题标题】:Why the difference of set-cookie after curl call in php为什么在php中curl调用后set-cookie的区别
【发布时间】:2015-05-10 02:42:33
【问题描述】:

我有两页

http://site.aspx?page=AddressData2&AddressID=298587,466579,66052

http://site.aspx?page=AddressData2&ShowPanel=EID

第二个链接意味着在第一个链接被访问后保持相同的 cookie/会话信息。

我通过以下方式存储了第一个 cookie:

$cookies    =array(
        $cookies[0]=>$cookies[1],
        "__utma"=>"250300755.603693956.1425821004.1425827777.1425854702.4",
        "__utmb"=>"250300755",
        "__utmc"=>"250300755",
        "__utmz"=>"250300755.1425821004.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)",
        "style"=>"A--"
);
$formattedCookies='';
foreach ($cookies as $key => $value) {
    $formattedCookies.=$key."=".$value."; ";
}
$formattedCookies   .=" path=/; HttpOnly ";

这与您将在浏览器的 cookie 存储中获得的相同 cookie 非常相似。

但是,在我以与第一个链接相同的方式访问第二个链接后

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER  ,1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Set the url
curl_setopt($ch, CURLOPT_URL,$url_search);
// Execute
$result=curl_exec($ch);

另外,对于访问第二个链接的 curl 调用,我在 curl_setopt($ch, CURLOPT_HEADER ,1); 下添加了这一行来设置我之前制作的 cookie,

curl_setopt($ch2, CURLOPT_COOKIE,$formattedCookies);

然而,结果却截然不同。

在第一个链接中,curl_exec($ch2) 的标头结果如下所示:

HTTP/1.1 302 Found
Date: Sun, 08 Mar 2015 23:28:34 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Location: site.aspx?page=AddressData2&AddressID=298587,466579,66052
Set-Cookie: ASP.NET_SessionId=grrmvt55muepmsqruxxqwfrl; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 201

HTTP/1.1 200 OK
Date: Sun, 08 Mar 2015 23:28:34 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Set-Cookie: ASP.NET_SessionId=scmi1gvhrw5pwh45nv0hw1j3; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 17170

虽然,在第二个链接中,结果是这样的:

HTTP/1.1 302 Found
Date: Mon, 09 Mar 2015 00:01:19 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Location: site.aspx?page=error
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 156

HTTP/1.1 200 OK
Date: Mon, 09 Mar 2015 00:01:19 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Set-Cookie: ASP.NET_SessionId=dssnqq45hytyvy55kl3na455; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 16335

您可以看到 Set-Cookie 和 Location 之间存在很大差异,在第二个链接 curl 结果中,该 Location 成为错误页面,而标题的第一部分不存在 set-cookie。

不知道我哪里做错了?

【问题讨论】:

    标签: php curl cookies header web-crawler


    【解决方案1】:

    CURLOPT_COOKIESESSION 不是设置请求的 Cookie: 标头的选项。 CURLOPT_COOKIE 是。也就是说,path=/; HttpOnly 永远不应该成为其中的一部分。

    由于第二个 URI 期望会话 cookie 出现在请求中,而不是因为您没有设置它,它会将您重定向到错误消息页面。

    与其尝试手动处理 cookie,不如使用单个 curl 句柄并为两个请求设置 CURLOPT_COOKIEJAR 选项。

    【讨论】:

    • 但是在我访问第一页之前我没有会话,我认为 cookiejar 在我访问任何页面之前将固定的 cookie 信息存储在静态文件中......我想 CURLOPT_COOKIE 更好在这种情况下?你怎么看?
    • 另外,我尝试使用 COOKIE 而不是 COOKIESESSION,但收到了相同的结果...您认为具有“.utmccn=(direct)|utmcsr=(direct)| 的 cookie 之一是什么” utmcmd=(none)" 是否会与已经用于将值分配给 cookie 参数的 = 冲突?
    • CURLOPT_COOKIEJAR 确实将 cookie 保存在 curl_close() 上的文件中,但它也使 curl 自动管理 cookie(即读取响应 Set-Cookie: 并设置请求 Cookie: 标头)。因为在你的情况下,第一个 URI 设置了第二个需要的会话 cookie,这就是你想要的。
    • 哇!它起作用了:D 一个简单的问题......有没有办法告诉 php 做一些事情......就像它的 javascript 一样?因为在第二个链接中有这个 Show All 按钮,它使用 javascript 运行一种 C# 脚本......我应该如何真正将这个函数中的这个参数正确地传递给 php?请参阅stackoverflow.com/questions/3591634/how-to-use-dopostback 了解带有按钮的 javascript 函数
    • 好吧,我猜它会延伸到一些不同的麻烦领域,让你得到一分是不公平的。干杯!
    猜你喜欢
    • 1970-01-01
    • 2017-03-04
    • 2011-09-14
    • 2011-05-27
    • 2013-10-18
    • 1970-01-01
    • 2015-11-17
    • 2011-07-05
    • 2010-10-26
    相关资源
    最近更新 更多