php 如何在 codeigniter 中检查请求是 POST 还是 GET 请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26682920/
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 23:00:09  来源:igfitidea点击:

How can I check if request was a POST or GET request in codeigniter?

phpcodeigniterrequesthttp-posthttp-get

提问by hecontreraso

I just wondered if there is a very easy way to determine whether the request is a $_POSTor a $_GETrequest.

我只是想知道是否有一种非常简单的方法来确定请求是请求$_POST还是$_GET请求。

So does Codeigniterhave something like this?

那么Codeigniter有这样的东西吗?

$this->container->isGet();

回答by cOle2

I've never used codeigniter but for this I check the $_SERVER['REQUEST_METHOD'].

我从未使用过 codeigniter,但为此我检查了$_SERVER['REQUEST_METHOD'].

Looking at the docsmaybe something like:

查看文档可能类似于:

if ($this->input->server('REQUEST_METHOD') == 'GET')
   //its a get
else if ($this->input->server('REQUEST_METHOD') == 'POST')
   //its a post

If you're going to use it alot then its simple to roll your own isGet()function for it.

如果您要大量使用它,那么isGet()为它推出您自己的功能很简单。

回答by Wilco Waaijer

For CodeIgniter 3 users: the docs statethe input class has a function to get the request method:

对于 CodeIgniter 3 用户:文档状态输入类有一个函数来获取请求方法:

echo $this->input->method(TRUE); // Outputs: POST
echo $this->input->method(FALSE); // Outputs: post
echo $this->input->method(); // Outputs: post

回答by Inc33

In codeigniter 4:

在代码点火器 4 中:

$this->request->getMethod() // Returns get, post, or whatever the method is