Find out HTTP method in PHP [duplicate]

2019-01-13 07:10发布

This question already has an answer here:

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

标签: php http
2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-13 07:21
$_SERVER['REQUEST_METHOD']

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

查看更多
对你真心纯属浪费
3楼-- · 2019-01-13 07:30

While checking

$_SERVER['REQUEST_METHOD']

seems the obvious choice, since some of the people are advocating safe superglobals alternatives (Is using superglobals directly good or bad in PHP? and similar questions), one may actually use autosanitizing

filter_input( INPUT_SERVER, 'REQUEST_METHOD' )

(possibly with some additional filtering switches, eg. FILTER_SANITIZE_SPECIAL_CHARS) instead.

Of course, 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

查看更多
登录 后发表回答