Question
In PHP, how can I determine which HTTP request method was used for the current request, such as GET, POST, PUT, or DELETE?
Short Answer
By the end of this page, you will understand how PHP exposes the current HTTP request method, how to check for methods like GET, POST, PUT, and DELETE, and how this is commonly used in form handling, APIs, routing, and request validation.
Concept
PHP provides information about the current HTTP request through the $_SERVER superglobal. To detect the request type, you usually read $_SERVER['REQUEST_METHOD'].
$method = $_SERVER['REQUEST_METHOD'];
This value contains the HTTP method used by the client, such as:
GETPOSTPUTDELETEPATCHOPTIONS
This matters because web applications often behave differently depending on the request method:
GETusually retrieves dataPOSTusually creates or submits dataPUTusually updates or replaces dataDELETEusually removes data
In real programming, checking the request method is a basic part of:
- building APIs
- processing forms
- writing simple routers
- validating allowed actions
- returning proper error responses
The key idea is that the URL alone is not always enough to decide what the server should do. The HTTP method provides extra meaning about the action being requested.
Mental Model
Think of an HTTP request like a labeled delivery envelope.
- The URL tells you where the envelope should go.
- The request method tells you what kind of action the sender wants.
For example:
GET= “Please show me the information.”POST= “Please send this new information.”PUT= “Please replace or update this information.”DELETE= “Please remove this information.”
In PHP, $_SERVER['REQUEST_METHOD'] is the label on that envelope. Your code reads the label and decides how to handle the request.
Syntax and Examples
The basic syntax is:
$method = $_SERVER['REQUEST_METHOD'];
You can compare it with specific method names:
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
echo 'This is a GET request';
}
Example: Checking multiple request methods
<?php
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
echo 'Fetch data';
} elseif ($method === 'POST') {
echo 'Create data';
} elseif ($method === 'PUT') {
echo 'Update data';
} elseif ($method === 'DELETE') {
echo 'Delete data';
} else {
echo 'Unsupported request method';
}
This example reads the method once, stores it in $method, and then uses conditional logic to decide what action to take.
Example: Using switch
<?php
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
echo 'Handle GET';
break;
case 'POST':
echo 'Handle POST';
break;
case 'PUT':
echo 'Handle PUT';
break;
case 'DELETE':
echo 'Handle DELETE';
break;
default:
http_response_code(405);
echo 'Method Not Allowed';
}
This style is often cleaner when you have several possible methods.
Example: Detecting and reading a PUT body
<?php
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
$rawBody = file_get_contents('php://input');
echo $rawBody;
}
For PUT and DELETE, data is often sent in the raw request body rather than automatically appearing in $_POST.
Step by Step Execution
Consider this example:
<?php
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'POST') {
echo 'Form submitted';
} else {
echo 'Show form';
}
Step by step:
- PHP receives an HTTP request.
- The web server passes request details to PHP.
- PHP stores the request method in
$_SERVER['REQUEST_METHOD']. - The code copies that value into
$method. - The
ifstatement checks whether$methodis exactly'POST'. - If it is
POST, PHP printsForm submitted. - If it is not
POST(for exampleGET), PHP printsShow form.
Example trace
If the browser visits the page normally:
REQUEST_METHOD = GET
Result:
Show form
If a form submits data to the same page with method POST:
REQUEST_METHOD = POST
Result:
Form submitted
Real World Use Cases
Checking the request method is common in many PHP applications.
1. Form handling
A page may show a form on GET and process it on POST.
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
echo 'Display form';
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo 'Process submitted data';
}
2. REST-style APIs
An API endpoint may use the same URL with different methods:
GET /users/5→ fetch a userPUT /users/5→ update a userDELETE /users/5→ delete a user
3. Route protection
You may allow only certain methods for an endpoint.
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
exit('Only POST is allowed');
}
4. AJAX or frontend requests
JavaScript apps often send POST, PUT, PATCH, or DELETE requests to a PHP backend.
5. Webhooks
Webhook handlers often require a specific method such as POST for security and correctness.
Real Codebase Usage
In real projects, developers usually do more than just print the method. They combine request-method checks with validation, routing, and error handling.
Guard clauses
A common pattern is to reject invalid methods early.
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
exit('Method Not Allowed');
}
This keeps the rest of the code simpler.
Simple routing
A single endpoint may branch by method:
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
// return resource
break;
case 'POST':
// create resource
break;
default:
http_response_code(405);
}
Validation before processing
Developers often check both the method and the input data.
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
exit;
}
if (empty($_POST['email'])) {
http_response_code(400);
exit('Email is required');
}
Reading raw input for non-POST methods
In many PHP setups, PUT and DELETE data must be read manually.
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
$body = file_get_contents('php://input');
}
Method override support
Some frameworks or apps allow a POST request to pretend to be PUT or DELETE using a hidden field or header. This is common when HTML forms cannot directly send those methods.
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'POST' && isset($_POST['_method'])) {
$method = strtoupper($_POST['_method']);
}
This pattern should be used carefully and validated properly.
Common Mistakes
1. Using $_POST to detect the request method
Broken idea:
if ($_POST) {
echo 'This must be POST';
}
Why it is a mistake:
$_POSTbeing empty does not always mean the request was notPOST- the correct source for the HTTP method is
$_SERVER['REQUEST_METHOD']
Correct version:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo 'This is a POST request';
}
2. Forgetting that method names are uppercase
Broken code:
if ($_SERVER['REQUEST_METHOD'] === 'post') {
echo 'POST request';
}
Why it fails:
- PHP usually provides method names like
GETandPOSTin uppercase
Correct version:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo 'POST request';
}
3. Expecting PUT data in $_POST
Broken assumption:
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
$name = $_POST['name'];
}
Why it is a mistake:
$_POSTis typically populated for form-encodedPOSTrequests, not rawPUTbodies
Safer approach:
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
$rawBody = file_get_contents('php://input');
}
4. Not handling unsupported methods
Broken code:
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
echo 'Get data';
}
Problem:
- other methods are silently ignored
Better version:
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
echo 'Get data';
} else {
http_response_code(405);
echo 'Method Not Allowed';
}
5. Trusting method override values without validation
If you support _method, do not accept any random value.
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'POST' && isset($_POST['_method'])) {
$override = strtoupper($_POST['_method']);
if (in_array($override, ['PUT', 'DELETE'], true)) {
$method = $override;
}
}
Comparisons
| Approach | Best for | Notes |
|---|---|---|
if/elseif with $_SERVER['REQUEST_METHOD'] | Small scripts and simple logic | Easy for beginners to read |
switch ($_SERVER['REQUEST_METHOD']) | Multiple methods on one endpoint | Cleaner when many cases exist |
| Framework routing | Larger applications | Framework handles method matching for you |
GET vs POST vs PUT vs DELETE
| Method | Typical purpose | Common data location |
|---|---|---|
GET | Read data | Query string |
POST | Submit or create data | Request body, often in $_POST |
PUT | Update or replace data | Raw request body |
DELETE | Remove data | Sometimes raw request body, often no body |
if/elseif vs switch
| Style | Pros | Cons |
|---|---|---|
if/elseif | Simple and flexible | Can get repetitive |
switch | Cleaner for many exact values | Less flexible for complex conditions |
Plain PHP vs framework handling
| Approach | Pros | Cons |
|---|---|---|
| Plain PHP | Full control, easy to learn fundamentals | More manual work |
| Framework | Faster routing and validation | Hides some low-level details |
Cheat Sheet
// Get the HTTP request method
$method = $_SERVER['REQUEST_METHOD'];
Common checks
if ($method === 'GET') {
// read
}
if ($method === 'POST') {
// create or submit
}
if ($method === 'PUT') {
// update
}
if ($method === 'DELETE') {
// delete
}
Reject unsupported methods
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
exit('Method Not Allowed');
}
Read raw body for PUT or DELETE
$body = file_get_contents('php://input');
Key rules
- Use
$_SERVER['REQUEST_METHOD']to detect the method - Compare against uppercase strings like
'GET'and'POST' - Do not use
$_POSTto detect the method PUTandDELETEoften require readingphp://input- Return
405 Method Not Allowedwhen a method is not supported
Useful methods you may also see
PATCHOPTIONSHEAD
Common pattern
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
// handle read
break;
case 'POST':
// handle create
break;
default:
http_response_code(405);
echo 'Method Not Allowed';
}
FAQ
How do I check if a request is GET or POST in PHP?
Use $_SERVER['REQUEST_METHOD'] and compare it to 'GET' or 'POST'.
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// GET request
}
What does $_SERVER['REQUEST_METHOD'] return in PHP?
It returns the HTTP method used for the current request, such as GET, POST, PUT, or DELETE.
Can PHP detect PUT and DELETE requests?
Yes. PHP can detect them through $_SERVER['REQUEST_METHOD']. However, request body data for those methods often needs to be read from php://input.
Why is $_POST empty on a PUT request?
Because $_POST is generally populated for form-style POST requests, not for raw PUT request bodies.
Should I use if or switch to detect the request method?
Use if for simple cases and switch when handling several methods on the same endpoint.
What status code should I return for an unsupported request method?
Use 405 Method Not Allowed.
Can HTML forms send PUT or DELETE directly?
Standard HTML forms mainly support GET and POST. Many applications use method override patterns to simulate PUT or DELETE.
Mini Project
Description
Build a small PHP endpoint that behaves differently depending on the HTTP request method. This demonstrates how one URL can support multiple actions, which is a common pattern in APIs and backend services.
Goal
Create a PHP script that detects GET, POST, PUT, and DELETE requests and returns an appropriate message for each one.
Requirements
- Read the current request method using PHP.
- Return a different response for
GET,POST,PUT, andDELETE. - For
PUT, read and display the raw request body. - Return a
405status code for unsupported methods.
Keep learning
Related questions
Are PDO Prepared Statements Enough to Prevent SQL Injection in PHP?
Learn how PDO prepared statements prevent SQL injection in PHP, what they protect, and the mistakes that still leave MySQL apps vulnerable.
Can You Bind an Array to an IN Clause in PHP PDO?
Learn how PDO handles placeholders in IN() clauses, why arrays cannot be bound directly, and the safe PHP pattern to build dynamic queries.
Choosing the Right MySQL Collation for PHP and UTF-8
Learn how MySQL character sets and collations work with PHP, and how to choose a practical UTF-8 setup for web applications.