PHP cURL Cookies Example

Posted by Mike Lopez under PHP cURL
14 Comments
May 2008
20
01:01pm

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…

<?php
/* 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!


Share this Post through Social Bookmarking
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Tags: ,

14 Responses to “PHP cURL Cookies Example”

  1. george Says:

    nice example dude , thanks

  2. Mike Lopez Says:

    you’re welcome dude!

  3. Lectrician Says:

    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!

  4. Mike Lopez Says:

    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

  5. Ali Says:

    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!

  6. Mike Lopez Says:

    What’s your code?

  7. sikko Says:

    Thank YoU !!!! works fine !!

  8. Bin Says:

    Simple but effective example. Good work, thanks!

  9. Hang Says:

    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

  10. SlipperyPeet Says:

    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

  11. erdosam Says:

    Yeah, nice xample, dude.. Simple but short :D

  12. Feroz Says:

    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..

  13. Yogesh Says:

    Object Moved error in above cookies example for my url through

  14. xQmail.eu Says:

    Great tutorial, simple and to the point, thanks!

Leave a Reply