Jump to content

Sending a cookie using cURL


Instant87

Recommended Posts

Hey, I have the following script to grab a page from a site:

 

<?php
$ch = curl_init() or die(curl_error());
curl_setopt($ch, CURLOPT_URL,"http://example.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data1=curl_exec($ch) or die(curl_error());
echo curl_error($ch);
curl_close($ch);
echo "$data1";
?>

 

However, I am interested in grabbing a portion of a site that requires to be logged in to view. I know already that the site uses three cookies to store the session. What would I have to add to the script to have those cookies be sent also?

Link to comment
https://forums.phpfreaks.com/topic/152595-sending-a-cookie-using-curl/
Share on other sites

Use :

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

To store the cookie in a file. (Usually on the first request when you login)

 

Then :

curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

To use the cookie in your next request.

 

Of course you will need write access to the directory to create/write the file. I think you can also directly read/write http header to read/set cookie with curl but i never try this so far.

 

http://www.php.net/manual/en/function.curl-setopt.php

 

 

I have one more question regarding the cookiejar function. I have able to use the cookiefile function by only making my own cookie.txt file and uploading it. Since the cookies expire at the end of a session (which is short), how would I be able to "login" to the site automatically (the site has no "remember me" function). It is my understanding that I use the cookiejar, but I have no clue on how to use it. Any help? Thanks.

Re-login and get a new cookie each time, you have to emulate what a user would do.

 

<?php

/* Config ------------------------------------------------------------- */

   $timeout = 15;      /* timeout in seconds */
   $cookiefilename = "cookie.txt";      /* this isn't thread safe unless you find a way
                                           to make unique filename */

   
/* Login to the site and save the cookie ----------------------------- */
   $post = "account=blablabla&password=blablabla";
   $ch = curl_init("http://www.somesite.com/login.php");
   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
   curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
   curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefilename);
   curl_setopt($ch, CURLOPT_HEADER, true);
   $output = curl_exec($ch);
   curl_close($ch);

   /* Test the $output to be sure you get a HTTP code 200 (success)
      and look into the html to be sure you are logged */
      
/* do what you have to do on that site
   you can repeat this as many url you need to see
   use cookiejar again if the cookie change else use cookiefile 
   */
   $ch = curl_init("http://www.somesite.com/somefolder/somefile.php");
   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
   curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefilename);
   curl_setopt($ch, CURLOPT_HEADER, true);
   $output = curl_exec($ch);
   curl_close($ch);
   
   /* Test the $output to be sure you get a HTTP code 200 (success)
      and grab the html for whatever you need it */
      
/* logout ------------------------------------------------------------- */
   $ch = curl_init("http://www.somesite.com/logout.php");
   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
   curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefilename);
   curl_setopt($ch, CURLOPT_HEADER, true);
   $output = curl_exec($ch);
   curl_close($ch);
   
   /* Test the $output to be sure you get a HTTP code 200 (success)
      and look into the html to get a string that show you
      you have logout properly */
    
   /* delete the cookie file */ 
   unlink($cookiefilename); 
?>

 

It not tested but it should work or at least give you a basic idea how it's done. You need to add a lot of if to be sure the script won't crash, and it will need to be modified each time the target site change. That why it's always better to use API instead of that, but some site just don't provide a API so you have no choice sometime.

When I echo the first output, it echo's the login page, not the page that it is supposed to go to after you login. Here is the html for the site's login page in case the helps.

 

<div id="login" class="content_container">
<div id="user_name" class="field_container">
<div class="fieldheading">UserName:</div>

<div class="field"><input class="field" type="text" name="user_name" value=""></div>
</div>
<div id="password" class="field_container">
<div class="fieldheading">Password:</div>
<div class="field"><input class="field" type="password" name="password"></div>
<div class="forgot"><a href="/game/forgot.pl">Forgot user name or password?</a>
</div>
</div>

<br /><br />

<div id="submit">
	<input type="submit" name="action" value="Submit">
</div>
</form>
</div>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.