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.

CONTENTS

Detect HTTP requests using REQUEST method

You can use the following code snippet:

<?php

switch ($_SERVER['REQUEST_METHOD']) {
  case 'PUT':
    //do something with PUT
    break;
  case 'POST':
    //do something with POST
    break;
  case 'GET':
    //do something  with GET
    break;
  default:
    //unknown method 
    break;
}

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:

<?php
$method = getenv('REQUEST_METHOD');

switch ($method) {
  case 'PUT':
    //do something with PUT
    break;
  case 'POST':
    //do something with POST
    break;
  case 'GET':
    //do something  with GET
    break;
  default:
    //unknown method 
    break;
}

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:

<?php
$method = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_ENCODED);

echo $method;

switch ($method) {
  case 'PUT':
    //do something with PUT
    break;
  case 'POST':
    //do something with POST
    break;
  case 'GET':
    //do something  with GET
    break;
  default:
    //unknown method 
    break;
}

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