html如何向php中post数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html如何向php中post数据相关的知识,希望对你有一定的参考价值。

我有一个htmlphp,采用WebBrowser控件将html中的表单数据提交给php,一切正常。但是,这种方式首先要用WebBrowser控件的Navigate Url 命令先打开网页,然后对html表单中的域进行复制,然后再submit给php,这种方式很正常和稳定,但不足的是低效率。
然后采用这种方法: WebBrowser.Navigate "http://xxx.xxx.xxx/xxx.php?user=user&pwd=pwd",结果php接收到的数据全部为空值。要如何对html,或者php,或者什么方法,能实现 WebBrowser.Navigate "http://xxx.xxx.xxx/xxx.php?user=user&pwd=pwd"这种正常的结果呢?

通过form表单提交,method为post类型。
如果按照地址栏方式传参(这种http://xxx.xxx.xxx/xxx.php?user=user&pwd=pwd),php就得用$_GET方式接收传过来的值追问

就是说更改php的代码,将$user= $_POST["user"];改成$user= $_GET["user"];就行了?我试试看。原来这么简单。

参考技术A 用Ajax不就行了吗追问

对ajax不熟悉呀。

如何防止用户向 php 文件发送如此多的 post 请求

【中文标题】如何防止用户向 php 文件发送如此多的 post 请求【英文标题】:How can I prevent user to send so many post requests to php file 【发布时间】:2020-09-22 16:06:12 【问题描述】:

我有一个 HTML 文件,里面有一个表单。当这个表单提交时,它会向 PHP 文件发送一个 POST 请求。 PHP 文件创建与 MySQL DB 的连接并更新其中的一行。

问题是任何人都可以获取此 POST 请求并将其发送到 PHP 文件同时,当 PHP 收到这些请求时,它将在数据库中执行更新并破坏数据库。

如何阻止用户发送这些请求?如何更改我的代码并使其更安全?

非常感谢!

index.html

<form action="send.php" method="post">
    <input type="text" name="product">
    <button type="submit">Submit and Send</button>
</form>

和...

发送.php

<?php

$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'test';

$conn = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) 
    // If there is an error with the connection, stop the script and display the error.
    exit('Failed to connect to MySQL: ' . mysqli_connect_error());


$prod = $_POST['product'];
$date = date('d.m.Y');

$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, "INSERT INTO store (product, date_added) VALUES (?,?)")) 
    exit('MySQL Error');
 else 
    mysqli_stmt_bind_param($stmt, 'ss', $prod, $date);
    mysqli_stmt_execute($stmt);
    header('Location: index.html');
    exit();    


 ?>

我的数据库是这样的:

id  |  product  |  date_added |
--------------------------------
1   |  wood     |  01.01.2020 |
--------------------------------

【问题讨论】:

请给我们看代码 @nbk 对不起,我会补充。 google.com/recaptcha/intro/v3.html 同时 POST 请求究竟应该如何“破坏”数据库? @gre_gor 我的意思是,例如,我可以编写一个 Python 脚本,将随机的 1000 个发布请求发送到文件。它的数据库将充满随机垃圾。 【参考方案1】:

有一个使用fail2ban 的解决方案。请提供fail2ban的文档

您可以在代码中引入一行,在客户日志文件中添加一行,例如/var/log/mysites/somesite.log

以这样的方式:

 <?php 
 # ( we will use this function to determine the corect ip of the spammer)

 function getRealUserIp()
    switch(true)
      case (!empty($_SERVER['HTTP_X_REAL_IP'])) : return $_SERVER['HTTP_X_REAL_IP'];
      case (!empty($_SERVER['HTTP_CLIENT_IP'])) : return $_SERVER['HTTP_CLIENT_IP'];
      case (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) : return $_SERVER['HTTP_X_FORWARDED_FOR'];
      default : return $_SERVER['REMOTE_ADDR'];
    
 



[...]
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, "INSERT INTO store (product, date_added) VALUES (?,?)")) 
    exit('MySQL Error');
 else 
    mysqli_stmt_bind_param($stmt, 'ss', $prod, $date);
    mysqli_stmt_execute($stmt);
    $ip = getRealUserIp();  // <<<<<<<<
    error_log("Visitor from - $ip !", 3, "/var/log/mysites/somesite.log");   // <<<<<<
    header('Location: index.html');
    exit();    

[...]

在下一步中,您必须安装和配置 fail2ban

apt install fail2ban

那么你必须在你的 /etc/fail2ban/jail.conf 中添加一个部分并在最后添加这个

[your_app]
port = http,https
logpath = /var/log/mysites/somesite.log

那么你需要在/etc/fail2ban/filter.d/your_app.conf中插入一个过滤器

# Fail2Ban filter for your_app.conf, looks for failed access attempts

[INCLUDES]

# Read common prefixes. If any customizations available -- read them from
# common.local
before = common.conf

[Definition]

# Regexp to fit your logfile entry ... read the fail2ban documentation
# and customize as this is just a example
failregex = ^(*.) <HOST> !$
ignoreregex =

然后你回到控制台并打开怪物......

fail2ban-client stop
fail2ban-client add your_app
fail2ban-client start

fail2ban-client status your_app

【讨论】:

以上是关于html如何向php中post数据的主要内容,如果未能解决你的问题,请参考以下文章

如何防止用户向 php 文件发送如此多的 post 请求

php向mysql写入数据

如何向php服务器发送数据为json的post请求

php如何接收别的服务器post过来的数据 - 技术问答

如何用php向服务器发送post请求

MVC中前台如何向后台传递数据------$.get(),$post(),$ajax(),$.getJSON()总结