0

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

4
  • 2
    use $_SERVER['REQUEST_METHO' . 'D'], no but seriously can you explain why you can't use this global? Commented Dec 12, 2014 at 8:03
  • Why not use server var? Commented Dec 12, 2014 at 8:08
  • Can we know the reason?? Is it for something specific? Commented Dec 12, 2014 at 8:10
  • Yesterday I had an interview, interviewer asked almost same question Commented Dec 12, 2014 at 9:26

2 Answers 2

2

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 */ );
Sign up to request clarification or add additional context in comments.

2 Comments

What if there are URL and POST parameters?
@Gumbo touche. I updated the get clause with a statement checking the post to offer a third solution of 'both'.
1

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';

1 Comment

$_GET and $_POST are always set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.