简体   繁体   中英

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

I just wondered if there is a very easy way to determine whether the request is a $_POST or a $_GET request.

So does Codeigniter have something like this?

$this->container->isGet();

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

Looking at the docs maybe something like:

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

If you're going to use it a lot then it's simple to roll your own isGet() function for it.

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

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

在代码点火器 4 中:

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

The correct answer is:

if ($this->input->post()) {
   //have post
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM