duanlu0386 2013-10-05 06:37
浏览 55
已采纳

无法通过CURL PHP保留会话ID

I'm writing a function to fetch the captcha, of course it needs to keep the session. I'm using cookie file for curl and use it for every request but it does not work. When I view the cookie file, I see that the PHPSESSID changed each time I call the function. How could I solve it?

Here is my code

<?php    
function fetch_captcha($url, $cookie_file = false, $user_agent = DEFAULT_USER_AGENT, $timeout = 10) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

    if ($cookie_file) {
        curl_setopt($curl, CURLOPT_COOKIESESSION, true);
        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
        curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
    }
    curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);

    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}
  • 写回答

1条回答 默认 最新

  • douluo3256 2013-10-05 06:48
    关注

    Don't use:

    curl_setopt($curl, CURLOPT_COOKIESESSION, true);
    

    PHPSESSID is a session cookie, and this option causes each cURL call to start a new session, so it ignores all session cookies in the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?