Home » Php » php – When can there be data in both $_GET and $_POST

php – When can there be data in both $_GET and $_POST

Posted by: admin July 12, 2020 Leave a comment

Questions:

Is it actually possible to get data in both $_GET and $_POST? And how does this relate to what is in $_REQUEST?

How to&Answers:

Yes, it’s possible. Consider a form like this:

<form action="foobar.php?a=123&b=456" method="post">
    <input type="text" name="a" value="llama">
    <input type="text" name="b" value="duck">
    <input type="submit" name="go" value="Submit me!">
</form>

On submitting this form, $_GET["a"] == "123", $_GET["b"] == "456", $_POST["a"] == "llama", $_POST["b"] == "duck", and $_POST["go"] == "Submit me!".

How this relates to the $_REQUEST superglobal depends on the value of the request_order (or the older variables_order) PHP configuration setting, as the php.ini documentation explains.

Answer:

There can definitely be data in both… Consider the following (very simple) page:

<body>
  <form method="post" action="params.php?myparam=myval">
    <input type="text" name="param1"></input>
    <input type="submit" name="submit" value="submit" />
  </form>
</body>

Notice that the action of the form contains a query string, and the method is post. $_GET contains the query string params, $_POST contains the form params, and $_REQUEST contains the merged parameters from both arrays:

array(3) {
  ["myparam"]=>
  string(1) "myval"
  ["param1"]=>
  string(0) ""
  ["submit"]=>
  string(6) "submit"
}

Check out request_order for controlling how the super-globals are processed in $_REQUEST.

Answer:

It’s possible. The request_order or (if that’s unset) variables_order directive determines which will take precedence in $_REQUEST when a key is set in both.

Answer:

It’s possible in PHP because, despite their names, $_GET and $_POST don’t really need a GET or POST.

  • $_GET contains the querystring parsed as form encoded variables.
  • $_POST contains the request body parsed as form-encoded variables

It doesn’t matter what the actual request method is – it could be a PUT and those superglobals would still get populated.