What's the difference between $_POST, $_GET, and $_REQUEST?
Asked Answered
L

6

15

I am bit confused about these super global variable ($_POST, $_GET, and $_REQUEST) in PHP. In which scenario do I need to use these variables in PHP, and what are the main differences these three stand for?

Lingwood answered 22/3, 2017 at 4:4 Comment(6)
#3477833Lashondalashonde
Or the manual: php.net/manual/en/language.variables.superglobals.php - always a great resource ;-)Appraisal
It would be a good idea to never use $_REQUESTGeriatric
hi @suraj thanks for your reply, I tried that link, but it not included the request variableLingwood
that is regarding $GET and $POST in general with respect to http.. $_REQUEST as the answer will contain request values irrespective of type of request..Lashondalashonde
Does this answer your question? When should I use GET or POST method? What's the difference between them?Shorts
T
21

$_POST is an associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request. You can use when you are sending large data to server or if you have sensitive information like passwords, credit card details etc

$_GET is an associative array of variables passed to the current script via the URL parameters. you can use when there is small amount of data, it is mostly used in pagination, page number is shown in the url and you can easily get the page number from URL using $_GET

$_REQUEST is a 'superglobal' or automatic global, variable. This simply means that it is available in all scopes throughout a script. It is an associative array that by default contains the contents of $_GET, $_POST and $_REQUEST (depending on request_order=)

Tansey answered 22/3, 2017 at 4:26 Comment(3)
$_COOKIE shouldn't be stroken over. Either delete it or specify for which versions of PHP it is valid for (or some other explanation, whatever is true).Centenarian
they are all "superglobals". Full list: $GLOBALS, $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST, $_ENV as documented here: php.net/manual/en/language.variables.superglobals.phpDre
Something seems wrong with this answer, it says that "$_REQUEST (...) by default contains the contents of $_GET, $_POST and $_REQUEST". It contains itself? Other answers seem to imply that it should read "$_COOKIE" and not "$_REQUEST" in the end. But perhaps somebody more sure of this can edit the answer instead of me.Siebert
S
11

Difference is:

  • $_GET retrieves variables from the query string, or your URL.

  • $_POST retrieves variables from the request body, in case it was encoded using either application/x-www-form-urlencoded or multipart/form-data encoding.

  • $_REQUEST is a merging of $_GET, $_POST and $_COOKIE where order of overriding values with same keys is defined in PHP configuration.

Sesquicarbonate answered 22/3, 2017 at 4:5 Comment(5)
Actually the $_REQUEST behaviour is determined by the request_order and variables_order configuration directives.Geriatric
$_REQUEST Also includes values in the $_COOKIE super global ;) php.net/manual/en/reserved.variables.request.phpBarbette
Finally I think you are answering my question. If I have a POST [name] and a GET[name] if I did a REQUEST [name] it would = POST [name] - correct (out with a request_order)? I am beginning to think REQUEST ain't that useful. When would u use it?Renfred
Can you update your answer to qualify "where $_POST overrides $_GET" (as other comments have alluded to)?Centenarian
actually, "The default php.ini on your system as of in PHP 5.3.0 may exclude cookies from $_REQUEST." from a comment here: php.net/manual/en/reserved.variables.request.phpDre
S
10

There are 2 methods to send HTML form data from 1 Page to another or HTML page to server side (In PHP).

  1. POST

It is a method in which data gets sent using packet which is not visible to any user on web-browser. it is secured compared to GET method.

  1. GET

It is a method in which data gets sent with URL which is visible to user in address-bar of any web-browser. So, it’s not secure as POST method.

Now, There are total three super global variables to catch this data in PHP.

  1. $_POST: It can catch the data which is sent using POST method.
  2. $_GET: It can catch the data which is sent using GET method.
  3. $_REQUEST: It can catch the data which is sent using both POST & GET methods.

Also with $_GET superglobal variable can collect data sent in the URL from submit button.

Symbiosis answered 13/8, 2018 at 14:8 Comment(0)
P
1

Well, to know better, please visit GET vs. POST:

1) Both $_GET and $_POST create an array e.g. array( key => value, key2 => value2, key3 => value3, ...). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.

2) Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.

3) $_GET is an array of variables passed to the current script via the URL parameters.

4) $_POST is an array of variables passed to the current script via the HTTP POST method.

---- whereas $_REQUEST contains $_POST, $_GET and $_COOKIE .

Plasm answered 22/3, 2017 at 4:13 Comment(0)
D
-1

$GET : When you use GET method, your paramethers and requests will appear on URL adress $POST: If you use POST method, doesnt

Also POST methods safer than GET methods

Dierdre answered 21/3, 2024 at 10:57 Comment(0)
G
-2

$_POST hides the variables/values (they cannot be seen), whereas $_GET shows them! If the values include passwords or other sensible info, it's advisable to use $_POST.

Gott answered 21/3, 2024 at 10:38 Comment(2)
You are confusing php arrays with the way HTML forms handled by browser. Also, POST method doesn't actually hide anything.Luddite
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Fuscous

© 2022 - 2025 — McMap. All rights reserved.