Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a request is GET or POST [duplicate]

Tags:

php

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 GET or POST method.

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?

like image 722
Graviton Avatar asked Sep 03 '09 08:09

Graviton


People also ask

How can I tell if a POST request is successful in PHP?

Use isset() method in PHP to test the form is submitted successfully or not. In the code, use isset() function to check $_POST['submit'] method. Remember in place of submit define the name of submit button. After clicking on submit button this action will work as POST method.

How do I know if request is POST or laravel?

Or just use request()->isMethod('post') anywhere cause the function request() is registered globally in Laravel. Can be checked by request()->method === 'PUT' as well.

Are GET and POST identical?

If you are working RESTfully, GET should be used for requests where you are only getting data, and POST should be used for requests where you are making something happen.


2 Answers

Better use $_SERVER['REQUEST_METHOD']:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {     // … } 
like image 176
Gumbo Avatar answered Oct 22 '22 21:10

Gumbo


Use $_SERVER['REQUEST_METHOD'].

like image 28
KV Prajapati Avatar answered Oct 22 '22 21:10

KV Prajapati