Id收到此代码,一切正常,cookie被添加到txt文件中,进行登录,登录后显示登录页面一切正常。
我的问题是加载下一页。因此,我已经登录,制作了cookie,现在需要加载通常无需登录即可隐藏的下一页。
我不知道如何:(这是我的代码:
// options
$EMAIL = '####################';
$PASSWORD = '####################';
$cookie_file_path = "cookie.txt";
$LOGINURL = "####################";
$agent = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)";
// begin script
$ch = curl_init();
// extra headers
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
// basic curl options for all requests
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
// set first URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL);
// execute session to get cookies and required form inputs
$content = curl_exec($ch);
// grab the hidden inputs from the form required to login
$fields = getFormFields($content);
$fields['loginid'] = $EMAIL;
$fields['passwd'] = $PASSWORD;
// get x value that is used in the login url
$x = '';
if (preg_match('/myauthenticate\.jsp\?x=(\d+)/i', $content, $match)) {
$x = $match[1];
}
$LOGINURL = "####################/myauthenticate.jsp?x=$x";
// set postfields using what we extracted from the form
$POSTFIELDS = http_build_query($fields);
// change URL to login URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL);
// set post options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
// perform login
$result = curl_exec($ch);
echo $result;
function getFormFields($data)
{
$inputs = getInputs($matches[1]);
return $inputs;
}
function getInputs($form)
{
$inputs = array();
$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
if ($elements > 0) {
for($i = 0; $i < $elements; $i++) {
$el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
$name = $name[1];
$value = '';
if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
$value = $value[1];
}
$inputs[$name] = $value;
}
}
}
return $inputs;
}
所以这一切正常,但是现在我需要关闭该脚本,并开始下一个页面加载,以读取cookie文件以查看已登录页面的数据。
有人可以帮忙吗?(
答案 0 :(得分:0)
一个_JAR
选项用于在接收cookie时存储cookie,另一个_FILE
选项用于读取cookie,以便将它们发回。确保apache
用户对该文件具有读/写访问权限,例如。作为它的所有者(很可能是这里的罪魁祸首)。
因此,我会说:touch cookie.txt
和chown apache:apache ./cookie.txt
...在测试过程中,如果有任何更改,您可以在初始请求后执行cat ./cookie.txt
进行验证。>