简体   繁体   中英

phpbb3 curl login

I'm trying to make an autologin bot for phpbb 3.0.10 with curl. The forum returns "You have been successfully logged in." but after that when i try to go to another page in the site and get the source with file_get_contents or even if i wait the page to redirect me to index.php it agains ask me to login. The point is that i'm doing these test on localhost and the forum is on another server and that's why i use curl.. Here's my code:

<?php
$urltopost = "http://www.site.com/phpbb3eng/ucp.php?mode=login";
$fget = file_get_contents($urltopost);
$sid_a = explode('index.php?sid=',$fget);
$sid_b = explode('"',$sid_a[1]);
$sid = $sid_b[0];
$datatopost = array (
'username' => "bot",
'password' => "123456789asd",
'autologin' => "1",
'viewonline' => "1",
'redirect' => "./ucp.php?mode=login",
'sid' => "$sid",
'redirect' => "index.php?sid=$sid",
'login' => "login"
);

$ch = curl_init ($urltopost."&sid=$sid");
curl_setopt ($ch, curlopt_post, true);
curl_setopt ($ch, curlopt_postfields, $datatopost);
curl_setopt ($ch, curlopt_returntransfer, true);
curl_setopt($ch, curlopt_useragent, 'mozilla/5.0 (windows; u; windows nt 6.1; en-us; rv:1.9.1.2) gecko/20090729 firefox/3.5.2 gtb5');
$returndata = curl_exec ($ch);
$code = $returndata;
echo htmlspecialchars($code); //returns source with successful login
$code2 = file_get_contents("http://www.site.com/phpbb3eng/index.php"); 
echo "<br /><br /><br />".htmlspecialchars($code2); // return login form
?>

You have to set cookies. Check the documentation: http://php.net/manual/en/function.curl-setopt.php

curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");

Of course you have to make sure that the cookie.txt file exists and that you have write permission.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM