PHP Reference ยท Variables and Constants
PHP $_REQUEST superglobal
Reference for PHP $_REQUEST: how it merges $_GET, $_POST and $_COOKIE through request_order, why the order matters, and why explicit arrays are safer.
$_REQUEST is an array that merges request data from several sources. Which sources and in what order is controlled by the request_order setting, and when that is empty, by variables_order. The sample php.ini-development and php.ini-production files set request_order to "GP": $_GET first, then $_POST overrides it, and cookies are left out. Without an ini file the built-in default is empty, so variables_order decides and cookies are included too.
Because the contents depend on server configuration and the same key can come from several places, $_REQUEST is considered a poor choice. Read from the superglobal that matches how the data is supposed to arrive.
<?php $id = $_POST["id"] ?? null; // clear: from the body $page = $_GET["page"] ?? 1; // clear: from the URL
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
$_REQUEST $_REQUEST["name"]
Type: array<string|int, string|array>
Sources are controlled by the ini settings:
| Setting | Effect |
|---|---|
request_order |
Letters G (GET), P (POST), C (COOKIE). Later letters override earlier ones. If empty, variables_order is used. |
variables_order |
Which superglobals PHP fills at all (EGPCS). |
Usage notes
- Order is configuration. The same key in the query string and body gives different results on servers with different
request_order. Code that works on your machine may read a different value elsewhere. - Cookies may be mixed in. If
Cis part of the order, a cookie calledidcan overwrite a form field. Attackers can plant cookies on subdomains, so this is a real risk. - Hurts security. Treating a
GETlink as if it were a form submission weakens CSRF protection. State-changing actions should explicitly read$_POSTand verify a token. - Unavailable keys. As with every request array, missing keys raise a warning; use
??. - Not for uploaded files. Files are in
$_FILESonly. - Snapshot.
$_REQUESTis built once at startup. Changing$_GETlater does not update it. - Better alternatives. Read the specific source, or use
filter_input()withINPUT_GETorINPUT_POST.filter_input()has noINPUT_REQUEST. - Frameworks. Request objects in frameworks usually expose separate
queryandrequestbags for the same reason.
Related
$_GET,$_POST,$_COOKIE,filter_input()- Lessons: Superglobals, Forms, Security basics