How to check the request method
Short answer
The method is in $_SERVER['REQUEST_METHOD']:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// the form was submitted
}Rejecting other methods
If a page accepts POST only, the others get a 405 code:
<?php
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
header('Allow: POST');
exit('Method not allowed');
}Functions used in this recipe
- http_response_code — Sets or returns the response status code.
- header — Sends an HTTP response header.
- filter_input — Reads a value from the request and validates it at once.