Page 1 of 1

Curl and Cookies

Posted: Sun Feb 19, 2006 2:05 am
by anjanesh
feyd | This thread was split from here.


Searching for php / curl brought me here.
Once the session cookie is got , how is it possible to send it back to access another page on the site ?
Take for example this forum.

Code: Select all

$handle = curl_init();
curl_setopt ($handle, CURLOPT_URL, "http://forums.devnetwork.net/index.php");
.
// Set other params including username and password
.
$file_contents = curl_exec($handle); // This gets logged in
curl_setopt ($handle, CURLOPT_URL, "http://forums.devnetwork.net/privmsg.php?folder=inbox"); // Now goto another link which is a members link
// I know a redirect=somewhere.php will work in this case but Im looking to access many pages after logging in
echo $file_contents;
curl_close($handle);
Thanks

Posted: Sun Feb 19, 2006 9:15 am
by feyd
From what I remember, it would be sent as long as you specify the cookie-jar.

Posted: Sun Feb 19, 2006 10:10 am
by anjanesh

Code: Select all

<?php
$url = "http://forums.devnetwork.net/login.php"; // This was edited in my Edit Post - changed index.php to login.php
$Data = "username=username&password=password&login=Login";

$handle = curl_init();
curl_setopt ($handle, CURLOPT_URL, $url);
curl_setopt ($handle, CURLOPT_POST, TRUE);
curl_setopt ($handle, CURLOPT_POSTFIELDS, $Data);
curl_setopt ($handle, CURLOPT_HEADER, TRUE);
curl_setopt ($handle, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt ($handle, CURLOPT_RETURNTRANSFER, TRUE);
// curl_setopt ($handle, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt ($handle, CURLOPT_REFERER, "localhost");
curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt ($handle, CURLOPT_FAILONERROR, 1);
curl_setopt ($handle, CURLOPT_COOKIEFILE, "D:/xxx/cook.txt");
curl_setopt ($handle, CURLOPT_COOKIEJAR,  "D:/xxx/cook.txt");

$file_contents = curl_exec($handle);

curl_setopt ($handle, CURLOPT_URL, "http://forums.devnetwork.net/privmsg.php?mode=post");
$file_contents = curl_exec($handle);
echo $file_contents;
curl_close($handle);
?>
shows the login page - not the post msg page. The first curl_exec() did get me logged in - but the second one was being treated as one without the cookies being sent I guess. Thats why I thought I had to do the 2 curl_setopt ($handle, CURLOPT_COOKIE, $cookie_data); for the phpbb2mysql_data and phpbb2mysql_sid - but this I hoped could be sent automatically instead of me extracting it and then sending it back.

EDIT : It worked !