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!
Tags: PHP cURL, PHP Programming
April 1st, 2009 at 1:55 am
nice example dude , thanks
April 1st, 2009 at 3:41 am
you’re welcome dude!
April 1st, 2009 at 10:07 am
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!
April 3rd, 2009 at 3:18 am
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
April 8th, 2009 at 9:56 am
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!
April 8th, 2009 at 4:26 pm
What’s your code?
April 30th, 2009 at 4:58 pm
Thank YoU !!!! works fine !!
August 5th, 2009 at 8:32 pm
Simple but effective example. Good work, thanks!
September 29th, 2009 at 8:25 am
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
September 30th, 2009 at 4:39 pm
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
October 20th, 2009 at 12:32 pm
Yeah, nice xample, dude.. Simple but short :D
October 23rd, 2009 at 10:49 am
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..
November 24th, 2009 at 10:37 am
Object Moved error in above cookies example for my url through
December 16th, 2009 at 7:59 pm
Great tutorial, simple and to the point, thanks!
February 10th, 2010 at 2:16 pm
1. Is it nessesary to do two requests?
2. Can curl automaticaly remove expired cookies form file ?
February 13th, 2010 at 12:38 am
Thanks a lot for this, great simple tutorial that i will be testing right away!
February 15th, 2010 at 4:35 am
1. Yes. The first request sets the cookie.
2. Not sure, but I think it does it automatically.
February 15th, 2010 at 6:47 pm
Shouldn’t you unlink() the tempfile so as not to have a bunch of unused files laying around in your file system?
February 16th, 2010 at 4:02 am
Yup, that’d be a good idea.
April 22nd, 2010 at 10:38 am
Thank you very much!!! Excelent example!!!