PHP cURL cookies not saving on Windows

Article published Sunday, January 17th, 2010 at 1:48 pm

We were recently given a PHP application with the task of modifying it for increased efficiency. This application was originally written and executed only in a Linux environment. At Renowned Media, all of our development takes place on Windows based computers.

Well, the modifications were going well, until it came time to execute cURL against a page which stores cookies. We were using the following code for this:

$cr = curl_init($url);
 curl_setopt($cr, CURLOPT_COOKIEFILE, "./cookie.txt");
 curl_setopt($cr, CURLOPT_COOKIEJAR, "./cookie.txt");

But for some reason the cookie.txt file was not being updated. In an attempt at trouble shooting, we checked to see if Apache/PHP had write permissions on this file, so we touched the file using php (which updates the timestamp or creates a blank file if it does not exist):

touch("./cookie.txt");

PHP was able to create the file, however cURL was still not writing to it! The cURL library is distributed on Linux machines as a separate binary from PHP which can be executed on the command line, meaning that cURL was not built into PHP/Apache.

Finally we realized that perhaps PHP did not send cURL the current working directory of the script that it was in, making the relative paths being sent to cURL useless. But, nobody wants to hard code paths into their applications, this makes it harder to expand the program and difficult to find all paths when moving the script. So, we set up the script to pull the current full location of the script that was executing the cURL instance, which would be sent to cURL as the path to sake the cookies in. Here are the updates to the cURL function calls to make it execute properly on a windows machine:

$cr = curl_init($url);
 curl_setopt($cr, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies.txt");
 curl_setopt($cr, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/cookies.txt");

Tags: ,

Category: PHP Tutorials

We don't only write awesome web development tutorials, we also build awesome web software! Check out NeoInvoice, a time tracking, issue ticket, invoice making web app! It's 100% free for our readers!

4 Responses to “PHP cURL cookies not saving on Windows”

  1. [...] I was recently given a PHP application with the task of modifying it and making it more efficient. This application was originally written and executed only in a Linux environment. Personally, all of my development takes place on Windows based computers. Well, the modifications were going well, until it came time to execute cURL against a [. [...] Read the full article at the source. [...]

  2. snipes says:

    Thank you for this. Perhaps it is in the PHP docs but I never saw it. Indeed on Windows, curl in PHP doesn’t pass the correct directory and your solution above fixes the problem perfectly.

    Thank you.

  3. Buzzknow says:

    Ah, thanks a lot, so this is why i always get 0KB txt file when try to save cookie ..

    Thanks alot :)

  4. GrooN says:

    Brilliant, hadn’t thought of that.
    It makes so much more sense now, I couldn’t understand why it kept getting the cookies, while the cookie file I had was empty. Now I found the cookie file, which it actually used, in the root of where apache is located.

    Thanks!

Leave a Reply