PHP : How to use cookie jar with curl

If you want to use cookie jar with your curl code at PHP script, you'll need to use the below few lines with your curl code.


$cookie_file = "/tmp/cookie/cookie1.txt";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);


These small piece of code chunk will enable you to use the assigned text file as cookie jar. But remember few things. First of all, you have to have use the two lines to set curl options for your cookie jar.

Secondly, please put absolute path with your cookie file name. Otherwise it may not get the cookie file while browsing from browser. Few web server work, but still keep yourself at safe side.

Finally, and must, you have to give your apache user to write access on the cookie file. Otherwise you know, it will get error.

Let me know if I missed anything, or miss guided you. I'm a human too :-)

Comments