Ras4U
Ras4U

Reputation: 492

PHP detecting request type (GET, POST, PUT or DELETE) without using $_SERVER['REQUEST_METHOD']

How can I find server request type (GET, POST, PUT or DELETE) without using $_SERVER['REQUEST_METHOD'] from action page?

I am submitting page from abc.php form action page is action page.

I need to print which method used

Upvotes: 0

Views: 1599

Answers (2)

Ne Ma
Ne Ma

Reputation: 1709

Regular if statements

if(!empty($_GET)) { 
    $request = (!empty($_POST)) ? 'both get and post' : 'get';
} else if(!empty($_POST)) {
    $request = 'post';        
}
//... You get the picture

Edit: I added a ternary within the get check to solve a problem that Gumbo noted in the comments. You can have both GET and POST vars available as you can POST data to a url with get params, i.e. /forms/addFileToCompany/?companyId=23

And now because I am a complete filth, the most horrible ternary you have ever seen! Note this is just for a bit of fun and I really do not recommend using it.

$request = (!empty($_GET)) 
    ? (!empty($_POST)) 
        ? 'both post and get' 
        : 'get'
    : (!empty($_POST))
        ? 'post'
        : (/* Keep it going for whatever */ );

Upvotes: 2

Arvs
Arvs

Reputation: 322

There's a tricky way and a not so smart way I believe. Is to check it manually like for example:

if( isset($_GET) ) $request_type = 'GET Method'; 
elseif( isset($_POST) ) $request_type = 'POST Method';

Upvotes: 1

Related Questions