php – Curl无法正确识别cookie中的expires值

我正在尝试使用curl在pinterest.com上登录.我得到了以下请求 – 响应流程:

> GET-请求登录表单并抓取隐藏字段(csrftoken)
> POST-Request登录凭据(邮件邮件)和刮取csrftoken
>接收会话Cookie以进行登录

使用Curl,我可以看到发送和接收的以下标题

 GET /login/?next=%2F HTTP/1.1
 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
 Host: pinterest.com
 Referer:
 Accept: text/html,application/xhtml+xml,application/xml,*/*
 Accept-Language: de-de,en-us
 Connection: keep-alive

 HTTP/1.1 200 OK
 Content-Type: text/html; charset=utf-8
 Date: Tue, 10 Apr 2012 15:03:24 GMT
 ETag: "45d6a85f0ede46f13f4fc751842ce5b7"
 Server: Nginx/0.8.54
 Set-Cookie: csrftoken=dec6cb66064f318790c6d51e3f3a9612; Max-Age=31449600; Path=/
 Set-Cookie: _pinterest_sess="eJyryMwNcTXOdtI3zXcKNq0qznIxyXVxK/KqSsy3tY8vycxNtfUN8a3yc3E09nXxLPdztLVVK04tLs5MsfXNAopVpVf6VnlW+Qba2gIAuqgZIg=="; Domain=pinterest.com; HttpOnly; expires=Tue, 17-Apr-2012 15:03:24 GMT; Max-Age=1334675004; Path=/
 vary: Cookie, Accept-Encoding
 Content-Length: 4496
 Connection: keep-alive

因此,在步骤1之后,设置两个cookie csrftoken和_pinterest_sess.但是看看cookiejar文件(我使用CURLOPT_COOKIEFILE和CURLOPT_COOKIEJAR让curl处理cookie处理)显示如下:

   # netscape HTTP Cookie File
   # http://curl.haxx.se/rfc/cookie_spec.html
   # This file was generated by libcurl! Edit at your own risk.

   pinterest.com        FALSE        /        FALSE        1365519805        csrftoken        dec6cb66064f318790c6d51e3f3a9612
   #HttpOnly_.pinterest.com        TRUE        /        FALSE        -1626222087        _pinterest_sess        "eJyryMwNcTXOdtI3zXcKNq0qznIxyXVxK/KqSsy3tY8vycxNtfUN8a3yc3E09nXxLPdztLVVK04tLs5MsfXNAopVpVf6VnlW+Qba2gIAuqgZIg=="

首先要注意的是_pinterest_sess cookie行之前的#HttpOnly_.我只是假设卷曲处理得很好.但进一步观察,可以看到负值被设定为到期日:-1626222087

我不知道它来自哪里,因为cookie设置为“expires = Tue,2012年4月17日15:03:24 GMT”(未来约7天,从今天算起).

在下一个请求中,curl不会设置_pinterest_sess cookie:

 POST /login/?next=%2F HTTP/1.1
 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
 Host: pinterest.com
 Referer: https://pinterest.com/login/?next=%2F
 Cookie: csrftoken=dec6cb66064f318790c6d51e3f3a9612
 Accept: text/html,application/xhtml+xml,application/xml,*/*
 Accept-Language: de-de,en-us
 Connection: keep-alive
 Content-Length: 123
 Content-Type: application/x-www-form-urlencoded

 HTTP/1.1 302 FOUND
 Content-Type: text/html; charset=utf-8
 Date: Tue, 10 Apr 2012 15:05:26 GMT
 ETag: "d41d8cd98f00b204e9800998ecf8427e"
 Location: http://pinterest.com/
 Server: Nginx/0.8.54
 Set-Cookie: _pinterest_sess="eJzLcsspCy4NTclIjvAOrjQzyywoCChISgvLDi+2tY9PrSjILEottvUN8a3yc4k09gtxrfRLt7VVK04tLs5MAYonV/qGeFb4ZkWW+4LES4tTi+KBEv4u6UZ+WYEmvlm+QOxZ6R/iwoebEmgLAKNfJps="; Domain=pinterest.com; HttpOnly; expires=Tue, 17-Apr-2012 15:05:26 GMT; Max-Age=1334675126; Path=/
 vary: Cookie
 Content-Length: 0
 Connection: keep-alive

在响应中,设置了另一个_pinterest_sess cookie,因为curl没有发送最后一个.

目前,我不知道我做错了什么,或者curl是否无法正确解析cookie中的expires值.

任何帮助将不胜感激 :)

//编辑
还有一件事:
根据http://opensource.apple.com/source/curl/curl-57/curl/lib/cookie.c,函数curl_getdate()用于提取日期.该函数的文档列出了一些例子(http://curl.haxx.se/libcurl/c/curl_getdate.html):

>太阳报,1994年11月6日08:49:37 GMT
>周日,06-Nov-94 08:49:37 GMT
> Sun Nov 6 08:49:37 1994
> 1994年11月6日08:49:37 GMT
>格林威治标准时间06-Nov-94 08:49:37
> 1994年11月6日08:49:37
> 1994年11月6日08:49:37
> 06-Nov-94 08:49:37
> 1994年11月6日08:49:37 GMT
> 08:49:37 06-Nov-94
>周日94年11月6日08:49:37
> 1994年11月6日
> 94年11月6日
>太阳11月6日94
> 1994.Nov.6
>太阳/ 11月6日/ 94 / GMT
>太阳报,1994年11月6日08:49:37 CET
> 1994年11月6日美国东部时间08:49:37
>太阳,2004年9月12日15:05:58 -0700
>周六,2004年9月11日21:32:11 0200
> 20040912 15:05:58 -0700
> 20040911 0200

它们都不匹配上述过期日期“Tue,17-Apr-2012 15:03:24 GMT”,因为所有带连字符的例子仅使用2位数年份.

解决方法:

由于32位有符号整数值的限制,您在计算机上遇到问题.

服务器将来设置一个Max-Age为1334675004秒的cookie.

Max-Age=1334675004

在这里发表了你的问题@ 2012-04-10 15:13:24Z.这是UNIX时间戳1334070804.如果您向它添加1334675004并且在进行整数往返时考虑32位整数限制2147483647,您将得到:-1626221485:

  1334070804
+ 1334675004
------------
 -1626221485

如数字所示,看起来服务器确实误解了Max-Age属性,如果你减去彼此的每个值,就会有一个7天的大约三角形(604200 = ~6.99天,差异是因为cookie比你在这里发布你的问题早了.)但Max-Age是秒的增量,而不是绝对的UNIX时间戳.

尝试使用PHP版本提高PHP_INT_MAX,或者针对64位编译,这应该可以防止负数.但是,服务器仍然会破坏max-age计算.您可能需要联系pinterest.com并报告问题.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...