CURL no longer sets cookies, but why?

My cURL script no longer works (so keep in mind that it worked before) on my localhost (so that it works on my external host, therefore: it could be server settings):

This script worked fine up to my local host (it still works on my host). Nothing changed.

  • Maybe the fact that I ran this script more than 3,000 times on my localhost is useful to know.
  • I am running Windows 7 using WampServer to install the host.
  • I may have changed the setting that affects the spelling of cookies. But which one?

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

Please note that I already use:

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

And what php.ini is:

 extension=php_curl.dll is uncommented 
  • Side question: Does curl_close end a cookie? What if cookiejar is not set?
  • The main question: Why donโ€™t you twist writing cookies, how it should do it (and does it on my external host, and not on my LOCALHOST.

Additional 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 used:

 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. 
+4
source share
4 answers

You were probably locked out during the login process. This will give you more information about the problem:

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

EDIT: some others choose what you can try (SSL solution resolved my problems more than once):

 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: Once selected, you will get returned headers (use only for debugging). Also, make sure that cookie.txt is used correctly (locally and writable).

 curl_setopt($curl, CURLOPT_HEADER, true); 

Thatโ€™s all I can do on my part. And now lunch!


EDIT 3: Cookie Related Material:

 $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 get this error, activate curl in php.ini uncomment / delete front panel ;

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

and restart the web server. If you did not find this line, you need to install curl:

 // ubuntu sudo apt-get install php5-curl // centos sudo yum install php5-curl 

For windows or your wampserver should be easy too.

+6
source

Despite the fact that this question is outdated, today I have the same problem, and I did not solve it with any of the suggestions. The reason cookies were not saved is simply a missing call

 curl_close() 

If curl_close is not called after a curl request, the cookie is not saved.

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

+10
source

There are two things that I see that are wrong in this code:

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

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

You made a request to submit, but did not specify the type of content, you must do the following:

 $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' )); 
+1
source

Two problems occurred in my situation: 1) The server did not accept cookies without a value 2) I did not use the CURLOPT_COOKIEFILE parameter

Each problem is solved by dividing it into separate parts and solving each of them in itself.

Therefore, I recommend that you split the problem: 1) Check this problem or the COOKIEFILE website - send the cookie value directly without a file - maybe the server is problem 2) If this is normal, check the contents of the file, maybe something is wrong (as in my situation)

-one
source

Source: https://habr.com/ru/post/1392316/


All Articles