我在这里搜索了有关以下内容的十几个答案:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
}
Run Code Online (Sandbox Code Playgroud)
然而,我仍然没有找到一个答案,为什么。
如果我们已经设置了,为什么要这样做<form method="post">?
这不是说这是这里唯一的表单方法吗?
如果用户来自以前的表单,则请求方法的确是POST。但是任何人都可以例如通过CURL或自定义程序向您的服务器发出请求。没有停止的人会随机请求您的页面。
因此,您不能确定服务器上的请求方法确实是POST,并且所有数据都存在。
在另一种情况下,它可以用于检查表单是否已实际提交。例如:
<?php if($_SERVER['REQUEST_METHOD'] == 'POST') { ?> <!-- The server has recieved something via POST! -->
Thank you for submitting the form!
<?php } else { ?> <!-- No postdata, lets show the form! -->
<form method='POST'> <!-- By setting the method we ask that the client does a post request. -->
<input type='submit' />
</form>
<?php } ?>
Run Code Online (Sandbox Code Playgroud)