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
Check whether a request is GET or POST
提问by Graviton
Possible Duplicate:
PHP detecting request type (GET, POST, PUT or 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.
我有一个脚本,在脚本中我想确定请求是否通过GET或POST方法到达。
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') {
// …
}

