Jump to content

cURL to Login to Forum


irishdaddy

Recommended Posts

I'm brand new to using cURL and I am trying to log into a vBulletin based forum (just like this one).

At this point I can get it to say "Thanks for logging in [username], wait while we redirect you", but once I get to the forums, the script is obviously not logged in anymore.

So it is as if the server OK's the authentication, but once I get to the forums it doesn't know I'm logged in. This leads me to believe it is a *cookies* problem.

Here is what I have as my code:

[CODE]
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);

$source = "http://forumname.com/forums";
$contents = file_get_contents($source);
echo $contents;
[/CODE]

I chmodded my cookie.txt to (777), but  they are empty after the script runs.

Any ideas? This has been *KILLING* me ALL day yesterday and today. I will name my first born after whoever can fix this for me.
Link to comment
https://forums.phpfreaks.com/topic/30048-curl-to-login-to-forum/
Share on other sites

[code]Set-Cookie: s_vi_x7Fbfnhddx60bel=[CS]v4|457A11C0000056CD-A160B3100003DE9|457B0F10[CE]; Expires=Thu,  8 Dec 2011 19:36:40 GMT; Domain=.2o7.net; Path=/[/code]

Is what came out when I used "liveHeaders" with a normal login to the site.

So I just dump this line into my cookie.txt file? I tried that.

Do I need to modify my code any?
curl_setopt has this other parameter you can try:
CURLOPT_COOKIESESSION

TRUE to mark this as a new cookie "session". It will force libcurl to ignore all cookies it is about to load that are "session cookies" from the previous session. By default, libcurl always stores and loads all cookies, independent if they are session cookies are not. Session cookies are cookies without expiry date and they are meant to be alive and existing for this "session" only.  Available since PHP 5.1.0. 
mr at coder dot tv
14-Apr-2006 07:22

Sometimes you can't use CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE becoz of the server php-settings(They say u may grab any files from server using these options). Here is the solution

1)Don't use CURLOPT_FOLLOWLOCATION
2)Use curl_setopt($ch, CURLOPT_HEADER, 1)
3)Grab from the header cookies like this:
preg_match_all('|Set-Cookie: (.*);|U', $content, $results);   
$cookies = implode(';', $results[1]);
4)Set them using curl_setopt($ch, CURLOPT_COOKIE,  $cookies);

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.