Find out HTTP method in PHP

PhpHttp

Php Problem Overview


How can I find out which method (usually GET or POST) is used for the current request?

Php Solutions


Solution 1 - Php

$_SERVER['REQUEST_METHOD']

See the docs. It will contain the request method upper-cased (i.e. 'GET', 'HEAD', 'POST', 'PUT').

Solution 2 - Php

While checking

$_SERVER['REQUEST_METHOD']

seems the obvious choice, since some of the people are advocating safe superglobals alternatives (https://stackoverflow.com/questions/3498207/is-using-superglobals-directly-good-or-bad-in-php and similar questions), one may instead use automatic sanitizing

filter_input( \INPUT_SERVER, 'REQUEST_METHOD', \FILTER_SANITIZE_SPECIAL_CHARS )

(you might of course use other filter, eg. FILTER_SANITIZE_STRING - see here for a full list).

Obviously, in the regular (GET/POST) case there ain't anything to sanitize, but a good habit is still a good habit IMO.

http://php.net/manual/en/reserved.variables.server.php

http://php.net/manual/en/function.filter-input.php

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioneWolfView Question on Stackoverflow
Solution 1 - PhpDominic RodgerView Answer on Stackoverflow
Solution 2 - Phpuser719662View Answer on Stackoverflow