当会话 Cookie 过期时使用 cURL
When Session Cookies Expire Using cURL
通常,在使用浏览器时,会话 cookie 会在浏览器 window 关闭时过期。
但是当使用 (php) cURL(并设置 COOKIE_FILE
和 COOKIE_JAR
选项时),它们能存活多久?
根据mozilla.org:
session cookie [...] is deleted when the client shuts down, because it didn't specify an Expires or Max-Age directive. However, web browsers may use session restoring, which makes most session cookies permanent, as if the browser was never closed.
根据documentation of curl_setopt
函数:
By default, libcurl always stores and loads all cookies, independent if they are session cookies or not. Session cookies are cookies without expiry date and they are meant to be alive and existing for this "session" only.
如果您将 cookie 保存在指定文件中
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://whosebug.com');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
$output = curl_exec($ch);
curl_close($ch);
然后,从客户端的角度来看,只要 CURLOPT_COOKIEJAR
设置了正确的 cookie,会话就会处于活动状态。这是您的脚本的选择。
通常,在使用浏览器时,会话 cookie 会在浏览器 window 关闭时过期。
但是当使用 (php) cURL(并设置 COOKIE_FILE
和 COOKIE_JAR
选项时),它们能存活多久?
根据mozilla.org:
session cookie [...] is deleted when the client shuts down, because it didn't specify an Expires or Max-Age directive. However, web browsers may use session restoring, which makes most session cookies permanent, as if the browser was never closed.
根据documentation of curl_setopt
函数:
By default, libcurl always stores and loads all cookies, independent if they are session cookies or not. Session cookies are cookies without expiry date and they are meant to be alive and existing for this "session" only.
如果您将 cookie 保存在指定文件中
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://whosebug.com');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
$output = curl_exec($ch);
curl_close($ch);
然后,从客户端的角度来看,只要 CURLOPT_COOKIEJAR
设置了正确的 cookie,会话就会处于活动状态。这是您的脚本的选择。