PHP cURL Cookies Example
Want to learn how to use PHP cURL with Cookies? Well, it’s pretty simple so let’s go see the code but first let’s make a scenario…
Say we have a page called ‘cookiepage.php’ in a particular website that checks for the cookie set by the homepage and will only return the proper output if the cookie exists. How do you do it? Here’s how…
/* STEP 1. let’s create a cookie file */
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ("http://somedomain.com/");
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
/* STEP 3. visit cookiepage.php */
$ch = curl_init ("http://somedomain.com/cookiepage.php");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
/* here you can do whatever you want with $output */
?>
Just in case you weren’t able to catch what we did in the above example, here’s a short explanation.
- STEP 1 creates a temporary cookie file using the tempnam() function.
- STEP 2 loads the homepage and saves the cookie in our temporary cookie file. The key here is this line:
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
- STEP 3 loads cookiepage.php with the saved cookie with this line:
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
Sleek and simple ei? Hope this PHP cURL Cookie example helped you out!

nice example dude , thanks
you’re welcome dude!
I am developing a small script to add to my YaBB (PERL) forum.
I am running a form in PERL which Submits to a PHP script.
I then want the PHP script then return to the PERL forum with some form values. For this I wanted to use cURL.
Trouble is, when cURL opens the forum it is logged out.
I assume I need to tell cURL to send or use the cookie stored on the users machine when it sends the form data to the forum. Once the forum has done what it needs I would then use a header redirect back to the forum.
Could I use your code above for this? I am struggling to see how to open the local machines cookie?
Thanks for your time!
Hi Lectrician,
You can do it but you’ll need to make a few more calls to curl_setopt with the cookie-related options. More of this can be found at php.net/curl_setopt
Mike
Hi!
Im developing a login Script and Im using Curl like above. But for some Reasons its saving a part of my cookies in my “cookie.txt”, not all of them. Has anyone an idea?
thx!
What’s your code?
Thank YoU !!!! works fine !!
Simple but effective example. Good work, thanks!
Hi,
I’m develop a program to log in to a website… I use cURL to do it.
The problem is, only one account can logged in to the website. Can you help me with the cURL?
Thanks for your time
Great article! Another example of pulling cookies (and header) values directly into a variable dynamically without any file i/o can be seen here: http://elame.com/damnthatsannoying/?p=7
Yeah, nice xample, dude.. Simple but short :D
hi,
File on another host… calling form http://localhost/test.php
—————————————————————————————-
$url =”http://localhost/taiba/tfcsadmcp/index.php”;
$reffer = ‘http://localhost/taiba/tfcsadmcp/index.php';
$cookie_file_path = getcwd().”/tfcsadmcp/_files/cookie.txt”;
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 0);
//curl_setopt($curl, CURLOPT_REFERER, $reffer);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($curl, CURLOPT_COOKIE, session_name() . ‘=’ . session_id());
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, “username=admin&password=323123&pageaction=login&”);
$data = curl_exec ($curl);
$signupStr=getSignupString($data);
curl_setopt($curl, CURLOPT_URL,”http://localhost/taiba/tfcsadmcp/index.php?component=cman&page=wce.gall.php”);//set this URL to wherever the form submits to
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($curl, CURLOPT_POST,1);//yes we want to post
curl_setopt($curl, CURLOPT_POSTFIELDS,$signupStr);//tell it where to find our sign up string
$data=curl_exec($curl);
curl_close($curl);//close the session
echo $data;
NOTE: But I can’t access another pages after i login.. only the calling page i can access and the target web is using cookies and session.
Login is working prefect..
Object Moved error in above cookies example for my url through
Great tutorial, simple and to the point, thanks!
1. Is it nessesary to do two requests?
2. Can curl automaticaly remove expired cookies form file ?
Thanks a lot for this, great simple tutorial that i will be testing right away!
1. Yes. The first request sets the cookie.
2. Not sure, but I think it does it automatically.
Shouldn’t you unlink() the tempfile so as not to have a bunch of unused files laying around in your file system?
Yup, that’d be a good idea.
Thank you very much!!! Excelent example!!!
Your kids can jump and play at LOL Parties while you enjoy soccer, roller hockey, martial arts, Club One, or the High Five at the Plex restaurant at Silver Creek Sportsplex
I try your code but i can’t get capctha from phpbb :(
i’ve put header image/png but still not work …
thanks
You just made my year. THANKS!!! This is helpful.
Thanks for this solution I was trying to find this solution on the php.net websites this is exactly what I needed !!!
[...] execute post with curl php cookies example [...]