Skip to content
Tutorials Center
My learning

Topics

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
<?php
$id = $_POST["id"] ?? null;      // clear: from the body
$page = $_GET["page"] ?? 1;      // clear: from the URL

Try it

PHP Which source wins?

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.

PHP Why explicit arrays are safer

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

PHP
$_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 C is part of the order, a cookie called id can overwrite a form field. Attackers can plant cookies on subdomains, so this is a real risk.
  • Hurts security. Treating a GET link as if it were a form submission weakens CSRF protection. State-changing actions should explicitly read $_POST and verify a token.
  • Unavailable keys. As with every request array, missing keys raise a warning; use ??.
  • Not for uploaded files. Files are in $_FILES only.
  • Snapshot. $_REQUEST is built once at startup. Changing $_GET later does not update it.
  • Better alternatives. Read the specific source, or use filter_input() with INPUT_GET or INPUT_POST. filter_input() has no INPUT_REQUEST.
  • Frameworks. Request objects in frameworks usually expose separate query and request bags for the same reason.