php - 卷曲登录+登录页面
问题描述
我得到了这个代码,一切正常,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 文件的下一页加载以查看登录页面的数据。
有人可以帮忙吗:(
解决方案
一个_JAR
选项用于在接收 cookie 时存储它们,另一个_FILE
选项用于读取 cookie,以便将它们发回。确保apache
用户对该文件具有读/写访问权限,例如。作为它的所有者(这似乎很可能是这里的罪魁祸首)。
因此我会说:touch cookie.txt
并且chown apache:apache ./cookie.txt
......在测试期间cat ./cookie.txt
,如果有什么变化,你可以在初始请求之后进行验证。
推荐阅读
- android - Android AppCompatButton 在屏幕下部看起来确实很疯狂
- groovy - Groovy 类工厂模式给出了奇怪的错误
- video-streaming - 在网站 esp32 cam 中实现视频流
- azure - Azure VM Scaleset 自定义脚本扩展不起作用 - 可能无法获取 VM 标识?
- python - 从列表元素中删除模式并在 Python 中返回另一个列表
- javascript - history.push() 不能在 firebase 的身份验证中使用 react
- java - 如何强制执行一致的 Java 代码格式?
- html - 如何让两个浮动图像彼此相邻以与上方和下方的图像对齐?
- node.js - 盖茨比在 mozjpeg 上构建失败
- google-analytics - 通过 API 检索 Google Analytics 4 事件参数