php 检查请求是 GET 还是 POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1372147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:14:58  来源:igfitidea点击:

Check whether a request is GET or POST

php

提问by Graviton

Possible Duplicate:
PHP detecting request type (GET, POST, PUT or DELETE)

可能重复:
PHP 检测请求类型(GET、POST、PUT 或 DELETE)

This should be an easy one.

这应该是一件容易的事。

I have a script, and in the script I want to determine whether the request arrive via GETor POSTmethod.

我有一个脚本,在脚本中我想确定请求是否通过GETPOST方法到达。

What is the correct way to do it?

正确的做法是什么?

I am thinking of using something like this

我正在考虑使用这样的东西

if (isset($_POST)) {
    // do post
} else  {
    // do get
}

But deep in my heart I don't feel this is the right way. Any idea?

但在我的内心深处,我不觉得这是正确的方式。任何的想法?

回答by Gumbo

Better use $_SERVER['REQUEST_METHOD']:

更好地使用$_SERVER['REQUEST_METHOD']

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // …
}