cURL doesn't set a cookie anymore, but why?

SuperSpy picture SuperSpy · Jan 22, 2012 · Viewed 15.5k times · Source

My cURL script does not work anymore (so keep in mind it DID work before) on my localhost (so it DOES work on my external host, hence: it might be the server settings):

This script worked fine before on my localhost (it still does on my host). Nothing changed.

  • Maybe the fact that I've ran this script over 3000 times on my localhost is useful to know.
  • I'm running on windows 7, using WampServer to setup a host.
  • I might have changed a setting, which effects the writing of cookies. But which one?

REAL PROBLEM: cURL does not set a cookie! What apache modules should be ON for writing cookies (in a .txt file)? I'm running wampserver.

Please note I'm already using:

    curl_setopt($curlTable, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($curlTable, CURLOPT_COOKIEFILE, 'cookie.txt');

And that php.ini is:

extension=php_curl.dll is uncommented
  • Side question: Does curl_close unset the cookie? And if the cookiejar option is not set?
  • Main question: Why doens't curl write a cookie like it should do (and does on my external host, NOT on my LOCALHOST.

Other information:

phpinfo()

curl
cURL support        enabled
cURL Information    7.21.7
Age                 3
Features
AsynchDNS           Yes
Debug               No
GSS-Negotiate       Yes
IDN                 No
IPv6                Yes
Largefile           Yes
NTLM                Yes
SPNEGO              No
SSL                 Yes
SSPI                Yes
krb4                No
libz                Yes
CharConv            No
Protocols           dict, file, ftp, ftps, gopher, 
                    http, https, imap, imaps, ldap, pop3,
                    pop3s, rtsp, scp, sftp, smtp, smtps, telnet, tftp
Host                i386-pc-win32
SSL Version         OpenSSL/0.9.8r
ZLib Version        1.2.5
libSSH Version      libssh2/1.2.7 

Currently using:

preg_match('/name="csrf" value="(.*?)"/', $getTokenCurlData, $token);

$postFields = array(
    'user'     => $userNum,
    'paswoord' => $userPass,
    'login'    => 'loginform',
    'csrf'     => $token[1]);

// 'user='.$userNum.'&paswoord='.$userPass.'&login=loginform&csrf='.$token[1]

$postData = http_build_query($postFields);

    $curlTable = curl_init();
    curl_setopt($curlTable, CURLOPT_URL, 'link');
    curl_setopt($curlTable, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($curlTable, CURLOPT_COOKIEFILE, 'cookie.txt');
    curl_setopt($curlTable, CURLOPT_ENCODING, 'gzip');
    curl_setopt($curlTable, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curlTable, CURLOPT_POST, true);
    $tableData = curl_exec($curlTable);
    if (!$tableData) echo 'post problem?'.$tableData;
    if ($tableData == false)
{
    echo 'Curl error: ' . curl_error($curlTable);
}

    curl_close($curlTable);
// Here I further process my data.

Answer

Alex2php picture Alex2php · Dec 10, 2012

Although this question is kind of outdated, i got the same problem today and did not solve it with any of the suggestions here. The reason the cookies were not saved was simply the missing call of

curl_close()

If curl_close is not called after the curl request cookies are NOT saved.

Took me about an hour to find out.... maybe saves your time :-)