【问题标题】:Request form and POST in a single go?一次请求表单和 POST?
【发布时间】: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 的页面。祝你好运,我一般不回答抓取问题

标签: php post


【解决方案1】:

Sup,你想要做的是不可能以这种方式使用 PHP,因为 PHP 的唯一工作就是在服务器端呈现你的内容。要收听客户端,您需要使用 javascript。

    let randomPassword = document.getElementById('a_randow_password')
    let name = document.getElementById('name')
    let password = document.getElementById('password')
    
    name.onkeyup = function(){
        password.value = randomPassword.value + name.value
    }
<input type="text" id="name" name="user_name" placeholder="name"><br />
<input type="hidden" name="a_random_password" id="a_randow_password" value="xxx">
<input type="text" id="password" placeholder="password"><br />

【讨论】:

  • 哦,这是一记耳光:/我想我可以“打电话给表格并询问随机密码,并在我挂断之前提供(发布)我的用户名和密码”
  • 哈哈,你可以做到这一点,你可以用 PHP 做到这一点,通过使用AJAX,它将在你的后端发出请求而无需重新加载页面。但是我们只在它是我们从数据库或其他复杂的东西中提取的东西时才使用它。我强烈建议您尝试将此值传递给 javascript。
【解决方案2】:

不确定你想要达到什么目的,但如果你想中断从 HTML 到 PHP 的请求(并修改|使用它),你需要 AJAX 请求 (JavaScript)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-16
    • 2018-11-22
    • 2015-06-22
    • 2013-02-19
    • 2017-10-13
    • 2022-04-21
    • 2020-08-09
    • 2020-05-30
    相关资源
    最近更新 更多