0

我正在尝试使用 curl 为 phpbb 3.0.10 制作一个自动登录机器人。论坛返回“您已成功登录”。但在那之后,当我尝试转到站点中的另一个页面并使用 file_get_contents 获取源时,或者即使我等待页面将我重定向到 index.php,它也会再次要求我登录。关键是我在 localhost 上进行这些测试,而论坛在另一台服务器上,这就是我使用 curl 的原因。这是我的代码:

<?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
?>
4

1 回答 1

3

您必须设置 cookie。检查文档: http: //php.net/manual/en/function.curl-setopt.php

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

当然,您必须确保 cookie.txt 文件存在并且您具有写入权限。

于 2012-10-20T21:40:45.907 回答