Check whether a request is GET or POST

Check whether a request is GET or POST

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Check Whether a Request is GET or POST

Determining the HTTP method of an incoming request is a fundamental task in web development, yet the implementation often reveals subtle misunderstandings about how PHP handles superglobals. A common pattern found in older tutorials or quick scripts suggests checking if the $_POST array is set:

if (isset($_POST)) {
    // do post
} else {
    // do get
}

While this logic seems intuitive, it fails to account for the specific behavior of PHP regarding empty arrays. If a form submits with no fields populated, $_POST is not null; it is an empty array ([]). Consequently, isset($_POST) evaluates to true, causing your code to incorrectly assume a POST request occurred when none did. This flaw can lead to logic errors where GET requests are processed as if they were POSTs, potentially causing data loss or incorrect state changes in your application.

The robust solution relies on the $_SERVER['REQUEST_METHOD'] superglobal. This variable is populated by the web server with the actual HTTP verb used to invoke the script, regardless of whether any data was sent in the body. It returns a string like 'GET', 'POST', 'PUT', or 'DELETE'. Using this method ensures you are checking the intent of the client rather than inferring intent based on the absence of input fields.

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Handle POST logic here
} else {
    // Handle GET logic (or other methods) here
}

This approach is more explicit and aligns with modern web development practices where you often need to distinguish between specific verbs like PUT or DELETE as well. If you are working within a framework like Laravel, the request() helper provides a convenient facade over this data:

if ($request->is('post')) {
    // Handle POST
} elseif ($request->is('get')) {
    // Handle GET
}

Laravel's routing system automatically maps HTTP verbs to controller actions, so you rarely need to manually check the method in every view or logic block unless you are building a raw API endpoint or handling specific legacy integrations. However, understanding the underlying $_SERVER variable remains crucial for debugging middleware issues or when writing non-framework native PHP scripts.

One significant pitfall to avoid is relying solely on the presence of data in $_POST. Some developers might check if a specific key exists (e.g., if ($_POST['submit'] === '1')) to infer a POST request. This is fragile because it assumes the form field always exists and is populated, which violates the principle of defensive programming. Always trust the HTTP protocol's method header.

Furthermore, consider the security implications. If you are handling sensitive data or performing state-changing operations, ensure that your application strictly validates the REQUEST_METHOD. For example, if a route should only allow GET requests for reading public data, explicitly rejecting non-GET methods prevents accidental or malicious modifications via POST or PUT requests targeting that same URL.

When integrating with external APIs or building RESTful services, checking $_SERVER['REQUEST_METHOD'] is standard practice. It allows your application to respond appropriately based on the client's action without making assumptions about data payloads. If you find yourself frequently switching between GET and POST logic in a single view file, it might be worth reconsidering your routing structure or separating concerns, as mixing read and write logic in one block often indicates a violation of separation of concerns principles.

Our network