Php check http request type

PHP: Detect if request type is POST or GET.

In this guide, we will show you how to detect the HTTP request type using PHP.

For example, is the user sending a GET or POST request? Or are they using another HTTP method such as PUT and DELETE?

Take a look at the following example:

//Get the request method from the $_SERVER $requestType = $_SERVER['REQUEST_METHOD']; //Print the request method out on to the page. echo $requestType;

As you can see, PHP will store the HTTP request method in the $_SERVER array.

Therefore, you can easily access it by referencing the “REQUEST_METHOD” element.

Knowing the request type is useful for a number of reasons:

  1. You can restrict certain URLs to specific request types. If they fail to use the correct method, then you can respond with a 405 code. For example, if you have a page that receives form data, then you might want to restrict that script to POST requests or PUT requests.
  2. It allows you to implement REST, which is a popular software architectural style that allows you to organize interactions between independent systems.
Читайте также:  Php checking if value is in array

Take a look at the following code, in which we handle different methods:

//Get the request method. $requestType = $_SERVER['REQUEST_METHOD']; //Switch statement switch ($requestType) < case 'POST': handle_post_request(); break; case 'GET': handle_get_request(); break; case 'DELETE': handle_delete_request(); break; default: //request type that isn't being handled. break; >

As you can see, PHP and its $_SERVER array make it pretty easy for us.

Источник

Detecting request type in PHP (GET, POST, PUT or DELETE)

In PHP, you can use the $_SERVER[‘REQUEST_METHOD’] superglobal to detect the request type. This superglobal will contain the request method used by the client, which can be one of GET , POST , PUT , or DELETE .

Here’s an example of how you can use this superglobal to detect the request type:

 if ($_SERVER['REQUEST_METHOD'] === 'GET') < // handle GET request > elseif ($_SERVER['REQUEST_METHOD'] === 'POST') < // handle POST request > elseif ($_SERVER['REQUEST_METHOD'] === 'PUT') < // handle PUT request > elseif ($_SERVER['REQUEST_METHOD'] === 'DELETE') < // handle DELETE request >

You can also use a switch statement to handle the different request types:

 switch ($_SERVER['REQUEST_METHOD']) < case 'GET': // handle GET request break; case 'POST': // handle POST request break; case 'PUT': // handle PUT request break; case 'DELETE': // handle DELETE request break; >

Источник

How to detect HTTP request type in PHP

In this article, I will discuss how to detect HTTP request types in PHP. But keep in mind that through this process we can only detect the request type. But to get GET or POST array value we need to use the $_GET or $_POST variable.

There are four main types of HTTP requests: GET, POST, PUT, and DELETE. In PHP, you can detect the type of HTTP request by checking the $_SERVER[«REQUEST_METHOD»] variable. So let’s see how we can detect HTTP requests in different ways.

Detect HTTP requests using REQUEST method

You can use the following code snippet:

Detect HTTP requests using getenv method

PHP’s getenv() function can be used to get the value of an environment variable. To get the type of HTTP requests use the following code:

Detect HTTP requests using REQUEST filter_input method

If you’re looking for a way to protect your website from malicious user input, then you might want to consider using the filter_input() function in PHP. This function can be used to filter all sorts of user input, including data from forms, cookies, and the $_GET and $_POST superglobals. To get the HTTP request type using this function, use the following code below:

The filter_input() function takes three arguments: the type of input to filter, the name of the variable to filter, and an options array.

The first argument can be one of several constants, such as INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.

The second argument is the name of the variable you want to filter.

The third argument is an options array that can be used to specify the desired filter or an array of flags.

Final Words:

In conclusion, if you need to detect the HTTP request type in PHP, you can use the $_SERVER[‘REQUEST_METHOD’] superglobal variable. This variable will give you the request method used by the client (e.g. GET, POST, etc.). You can then use this information to take different actions based on the request method.

About Ashis Biswas

A web developer who has a love for creativity and enjoys experimenting with the various techniques in both web designing and web development. If you would like to be kept up to date with his post, you can follow him.

Leave a Comment Cancel reply

Enjoying the articles? I would appreciate a coffee to help me keep writing and coding!

Источник

PHP — detect http request type in PHP (GET, POST, PUT, DELETE)

Leen-Kerr

In this article, we would like to show you how to detect the request method in PHP.

Server request method parameter example

Note: $_PUT and $_DELETES arrays are not supported in php it means php://input stream should be parsed.

        

Note: ajax.htm and backend.php should be placed on php server both.

GET request method example in PHP

POST request method example in PHP - Google Chrome Developer Tools

PUT request method example in PHP - Google Chrome Developer Tools

DELETE request method example in PHP - Google Chrome Developer Tools

References

Источник

Оцените статью