【发布时间】:2020-11-01 08:43:43
【问题描述】:
请原谅这个糟糕的标题。 我完全是初学者,不知道如何让它变得更好。
我正在尝试使用 PHP 发布表单数据。
我的问题是,在我发布表单数据之前,我需要从表单中获取一个值,每次我请求页面时都会改变。
请注意第二个输入,值是自动生成的,每次我请求表单时都是一个随机数。
这是我的form.php:
<?php
if(!isset($_REQUEST['submit_btn'])){
echo '
<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
<input type="text" name="user_name" id="user_name">
<input type="hidden" name="a_random_password" value="'.(rand(10,100)).'">
<input type="submit" value="submit" name="submit_btn">
</form>';
}
if(isset($_REQUEST['submit_btn']))
{
$user_name= $_POST["user_name"];
$a_random_password = $_POST["a_random_password"];
echo "Your User Name is:". $user_name;
echo "<br> and your Password is : $a_random_password;
}
?>
还有我的 post.php
<?php
$url = "http://test.com/form.php";
$data = array(
'user_name' => 'John',
'a_random_password' => 'xxx',
'submit' => 'submit'
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { echo "Error"; }
var_dump($result);
?>
那么我如何获取 a_random_password 值并将其与表单一起提交到单个请求中,否则密码将不适合用户名。
【问题讨论】:
-
你需要先刮一下
-
@LawrenceCherone 我尝试使用 curl 下载表单,但是当我上传表单时,密码已更改,因为我请求了两次表单。
-
你不会请求表单两次,使用 curl 使用 cookie jar 对 form.php 进行 http 请求(因为他们会将随机令牌存储在会话中。它实际上称为 CSRF 令牌),然后用 domdocument 解析 password 的页面,并且不要关闭 curl 连接,然后简单地创建另一个直接发布到 post.php 的页面。祝你好运,我一般不回答抓取问题