Curl and Cookies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Curl and Cookies

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

From what I remember, it would be sent as long as you specify the cookie-jar.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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 !
Post Reply