12

I'm trying to save a cookie in the curl cookiejar. I've simplified my code but its not working.

<?php

$cookie_file = './cookies.txt';

if (! file_exists($cookie_file) || ! is_writable($cookie_file)){
    echo 'Cookie file missing or not writable.';
    exit;
}//cookie_file is writable, so this is not the issue

$ch = curl_init (html_entity_decode("http://localhost/kiala_test/setcookie.php"));
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec ($ch);
echo $output;
?> 

setcookie.php

<?php 
$value = 'something from somewhere';
setcookie("TestCookie", $value);

?>

received header

HTTP/1.1 200 OK Date: Thu, 10 Oct 2013 12:10:37 GMT Server: Apache/2.4.4 (Win64) PHP/5.4.12 X-Powered-By: PHP/5.4.12 Set-Cookie: TestCookie=something+from+somewhere Content-Length: 0 Content-Type: text/html

so the testcookie is in the header but my cookiefile stays empty. What I'm doing wrong? what can I try to make this example work? thank you!

kasper Taeymans
  • 6,950
  • 5
  • 32
  • 51

4 Answers4

21

When setting CURLOPT_COOKIEJAR, you need to use an absolute path. You can do this easily by using:

curl_setopt ($ch, CURLOPT_COOKIEJAR, realpath($cookie_file) );

Reference: cannot use cookies in cURL PHP

Community
  • 1
  • 1
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • thank you! I've been struggling with this for a few hours now, even tried absolute paths but it wasn't working. realpath did the trick. thank you again. – kasper Taeymans Oct 10 '13 at 12:26
  • 2
    `realpath` returns absolute path, so you were doing something wrong, when you were struggling with this. – Glavić Oct 10 '13 at 12:27
  • 4
    CURLOPT_COOKIEJAR absolute path is required only for Windows platform. In Linux/Unix path can be relative. I checked it. – Ruslan Novikov Apr 07 '16 at 12:08
0

As reference about CURLOPT_COOKIEJAR:

The name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl_close.

So, in your code didn't used curl_close.

Use curl_close($ch); after curl_exec, for save cookies in jar file.

Nabi K.A.Z.
  • 9,887
  • 6
  • 59
  • 81
0

Yes realpath works, but remember that (thanks to php.net) before second call of curl_exec, CURLOPT_COOKIEFILE should be the same as in previous setting of CURLOPT_COOKIEJAR - because it is used for reading from "jared" file :)

//my working snippet for one php file:
//....
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('test2-cookie.txt'));
$content = curl_exec($ch);
curl_close($ch); 
$ch = curl_init();  
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('test2-cookie.txt'));
curl_setopt ($ch, CURLOPT_COOKIEFILE, realpath('test2-cookie.txt'));
$content = curl_exec($ch);//second call
0

I tried every thing and the what's worked for me was putting the absolute path for cookies file

 curl_setopt($ch, CURLOPT_COOKIEJAR, '/var/www/myapp/cookies.txt' ); 
 curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/myapp/cookies.txt' );
Salem
  • 654
  • 7
  • 24