Curl Cookies - Quick Tip

April 20, 2008

I know I haven’t got around to writing script that uses cookies to login. However if you’re on that path and are interested in using cookies to do things like login and stay logged into a site there’s one troubling thing I had big issues with when I started.

To use a cookie we need to set it’s location and this is where I’ve had troubles. For some reason I’ve had problems using a cookie not set with the full path. So when you set your cookie location make sure you use the full path of your cookie location.

$cookie = “/home/user/tmp/cookie.txt”;
If you’re going to use multiple threads or want to code up a captcha solve you have to save the session so you probably want to do a
$cookie = “/home/user/tmp/cookie-“.rand(111,9999).”.txt”;

And that’s the quick tip. I beat my head against the wall with this one a bunch of times and the full path seemed to be the thing that solved the curl cookie issues I had. If you’re having problems with cookies give this a try.

Just quickly to use cookies you just use a couple of simple lines.

So the initial call you need to set a cookie like this

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); // sets the cookie file location
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); // this says to start a new cookie file.

And that’s it to set the cookie on login or whatever you might need to start a cookie for. Then all your calls after that would look like this.

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); // sets the cookie file location
curl_setopt($ch, CURLOPT_COOKIE, $cookie); // use the cookie we have on file

And there you have it cookies are just that easy. I’ll get some example code up in the next day or so to show them in the wild.


Want More? The more people listening the more I’ll write.
Subscribe to get business insights in your inbox
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.