Encodera

Thanks for reading!

Donate to the site — we work hard for you.

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

Thanks for reading!

Donate to the site — we work hard for you.