PHP cURL cookies not saving on Windows

1 Comment
PHP Tutorials

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 page which stores cookies. I was 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, I checked to see if Apache/PHP had write permissions on this file, so I 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. Finally, I started thinking about the issue a little more. 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.

I finally realized that perhaps PHP did not send cURL the current working directory of the script that it was in, making my relative paths that I was sending 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, I 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 my 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");
  • Digg
  • Google Bookmarks
  • MySpace
  • Reddit
  • Facebook
  • NewsVine
  • StumbleUpon
  • Twitter
  • WordPress
  • Delicious
  • Yahoo Messenger
  • AIM
  • Bebo
  • Share/Bookmark

Related posts:

  1. PHP cURL Replacement If you are a PHP developer who writes a lot...
  2. Web Spidering Spidering, in its simplest form is the act of...
  3. Setting up VHOSTS using XAMPP/Apache in Windows Setting up a vhost allows you to use an alias...

One Response

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>