6

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.
SuperSpy
  • 1,324
  • 3
  • 13
  • 28

5 Answers5

12

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 :-)

Alex2php
  • 10,337
  • 1
  • 16
  • 22
  • warning, as of php8.0.0, curl_close() no longer flushes COOKIEJAR: https://github.com/php/php-src/issues/10387 – hanshenrik Jan 20 '23 at 13:19
7

Probably you was banned at the login process. This will give you more info about the problem:

// change 
// if (!$siteSource) echo 'Help!';
// to
if ($siteSource === false)
{
    echo 'Curl error: ' . curl_error($curl);
}

EDIT: some other opt that you can try (SSL related solved my problems more that one time):

curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_VERBOSE, 1);

// following is very important
// TRUE to follow any "Location: " header that the server sends
// as part of the HTTP header (note this is recursive, PHP will follow
// as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

EDIT 2: Following opt will give you the headers returned (use only to debug). Besides be sure about the cookie.txt is being used properly (locatable and writable).

curl_setopt($curl, CURLOPT_HEADER, true);

That's all I can contribute by my side. Now lo lunch!


EDIT 3: Cookie related stuff:

$cookie_file = PATH_TO_YOUR_COOKIE_FILE . '/cookie.txt';

if (! file_exists($cookie_file) || ! is_writable($cookie_file))
{
    echo 'Cookie file missing or not writable.';
    exit;
}

// you already added the following, I put it again just to remark their utility
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);

EDIT 4: Always check this at the beginning:

if ( ! extension_loaded('curl'))
{
    echo "You need to load/activate the curl extension.";
}

If you receive this error, activate curl in php.ini uncommenting/removing the front ;

windows:
;extension=php_curl.dll // or curl.dll
linux:
;extension=php_curl.so // or curl.so

and restart the web server. If you do not found this line then you need install curl:

// ubuntu
sudo apt-get install php5-curl

// centos
sudo yum install php5-curl

For windows or your wampserver it must be easy too.

Igor Parra
  • 10,214
  • 10
  • 69
  • 101
  • if ($siteSource === false) echo 'Curl error: ' . curl_error($curl); It is not false. Though this works: if ($siteSource == false) echo 'Curl error: ' . curl_error($curl); (one less equal sign) But there is no error given. So it outputs: 'Curl error: ' – SuperSpy Jan 22 '12 at 13:03
  • 1
    I added some ideas. I tell you that I have never lost a battle using curl so be persevering. Good luck... – Igor Parra Jan 22 '12 at 13:25
  • Added all of those. Didnt work. I've updated my question again. – SuperSpy Jan 22 '12 at 13:38
  • Using `===` is correct (don't change it). if works with `==` means that server returns nothing. That's why `curl_error` gives an empty string (no error occurred). Play with my last suggestions. Sure that all goes OK. – Igor Parra Jan 22 '12 at 13:43
  • WOOHAHAHA! New clue: It doesn't set cookies, which I/the other server NEED(S)! So WHY doesn't my wampserver set cookies? which things should be enabled? – SuperSpy Jan 22 '12 at 14:44
  • 1
    EDIT 3 done. I hope that all these EDITs are USEFUL. You know what I mean? 8) mmm? – Igor Parra Jan 22 '12 at 16:50
  • You sure earned it :) One more question: in php.ini, what settings should be switched on? And for Apache? – SuperSpy Jan 22 '12 at 17:44
  • EDIT 4 done. THX for your `check`. I think that we have checked all step needed. I'll check for feedback this evening. Cheers... – Igor Parra Jan 22 '12 at 18:17
  • The problem still is that it doesn't set a cookie though I provided all the options... Weird... – SuperSpy Jan 22 '12 at 19:47
  • @superspy is not weird. Check visually that you obtain a file called `cookie.txt`. You must check the path to the file and if it is writable (usually in windows that is not a problem). Seems that you are close to the solution. – Igor Parra Jan 23 '12 at 04:06
  • Well the stange thing is that I CAN write a cookie on my virtual apache host. But the SAME script can't on windows. The read-only check box of the folder 'cookies/' is a blue box. I cant change it. Neither as an admin nor as a normal user. What should the path be? If the folder is cookies: 'cookies/cookie.txt' Altough I've tried all of the possibilities already so I doubt it will work. – SuperSpy Jan 24 '12 at 15:59
1

I got this issue while migrating a script from PHP7 to PHP8

Prior to PHP8, the cookie wasn't set if we didn't call explicitely curl_close($handle).

With PHP8, this function has no effect anymore. We need to call unset($handle); to remove the CurlHandle object. Then, the cookie file is finally created.

Joffrey Schmitz
  • 2,393
  • 3
  • 19
  • 28
1

There are two things I can see that are wrong with that code:

You switch variables names between creating the resource and executing it:

$curl = curl_init();                 // $curl
$siteSource = curl_exec($curlTable); // $curlTable

You made a post request but didn't set a content type, you should do this:

$postFields = array(
  'user' => $userNum,
  'paswoord' => $userPass,
  'login' => 'loginform'
);
// This ensures all strings are properly encoded
$postData = http_build_query($postFields);

// ...

// curl *should* do this by itself, but best to do it explicitly
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  'Content-Type: application/x-www-form-urlencoded',
  'Content-Length: '.strlen($postData),
  'Connection: close'
));
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • The first is just a acquisition error. In my code this is done correctly. I've edited my post. Thanks for noticing. Also I included the build function, thanks. Added the header option as you provided. But still a blank page. (it returns false) – SuperSpy Jan 22 '12 at 12:49
  • Are you able to provide the actual URL of the site your are trying to access in order that I can test it myself? – DaveRandom Jan 22 '12 at 12:52
-1

In my situation there been two problems: 1) Server did not accepted cookie without value 2) I did not used CURLOPT_COOKIEFILE option

Every problem solves by dividing it on separate parts and solve each of it by itself.

So I recommend you to divide your problem: 1) Check is this COOKIEFILE problem or website - send cookie value directly without file - may be there is server problem 2) If this will be ok - check file contents may be there is something wrong (like in my situation)

Tomasz Stanczak
  • 12,796
  • 1
  • 30
  • 32
tigerxml
  • 1
  • 2