Lexib0y
Lexib0y

Reputation: 500

$_SERVER['REQUEST_METHOD'] and simultaneous POST and GET

When I use POST and GET requests at the same time, the $_SERVER['REQUEST_METHOD'] is set to just POST.

Why is this? Is it because all requests are considered GET in any case?

This is the request I made for the purpose of this question.

a = $("#AdminAddForm").serialize();
jQuery.post('index.php?test=yes', a);

Both $_POST and $_GET are populated after this request, and $_SERVER['REQUEST_METHOD'] set to POST.

Upvotes: 0

Views: 478

Answers (2)

pid
pid

Reputation: 11597

The HTTP protocol has a first line that is called the "request line". A post looks like this:

POST http://website.com/route/whatever HTTP/1.1

... (post body)

Notice the mandatory empty line between the request line and the post body.

Now, when you also have a query string like this:

POST http://website.com/route/whatever?q=hello HTTP/1.1

... (post body)

You're mixing these things:

  • the method POST;
  • the body of the POST (containing the form's content);
  • the query string.

The HTTP request IS a POST but in PHP the stuff in the query string will end up in the $_GET global variable nonetheless.

You can have GET parameters in a HTTP POST because the HTTP protocol allows to mix the POST body with the query string.

Upvotes: 5

Quentin
Quentin

Reputation: 943537

When I use POST and GET requests at the same time

This is impossible.

You are, probably, making a POST request that has a query string on the URL.

PHP will populate $_GET with data from the query string, but this has absolutely nothing to do with the request method. It is just one of PHP's weird (wrong) naming conventions.

Upvotes: 4

Related Questions