Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
HTTP Methods and Forms  ›  Capstone Project

$_REQUEST and $_SERVER

$_SERVER 13 min$_REQUEST merges GET, POST and possibly cookie values and hides where input came from, while $_SERVER describes the request, and its header-based values are written by the client and must be treated as untrusted.

$_REQUEST and $_SERVER

Pageturn's order script already reads $_POST and asks $_SERVER for the request method. PHP fills in a few more of these superglobals on every request. Two of them come up constantly: $_REQUEST, which is convenient and usually a mistake, and $_SERVER, which describes the request itself and deserves careful handling.

The rule

Read input from the source you expect; treat request details as claims.

$_REQUEST merges several sources into one array: $_GET, $_POST and, depending on configuration, cookies. When the same name appears in more than one, the server's request_order setting decides which one wins.

<?php
$qty = $_REQUEST['qty'] ?? 1;   // from the URL? the form? a cookie? unclear

That vagueness is the problem. An action that should only happen on a POST can be triggered by a plain link with ?qty=50, and a cookie can silently override a form field. Read $_GET or $_POST directly, so the code states where the data must come from.

$_SERVER holds information about the request and the environment:

Key Pageturn example
REQUEST_METHOD "POST"
REQUEST_URI "/search.php?q=dune"
QUERY_STRING "q=dune"
HTTP_HOST "pageturn.example"
HTTP_USER_AGENT the visitor's browser description
REMOTE_ADDR the IP address the connection came from
SCRIPT_NAME "/search.php"

The keys beginning with HTTP_ are copied from the request headers, and the client writes those headers. A script can send any User-Agent or Host it likes. So:

  • Use REQUEST_METHOD freely for branching; an unexpected value just fails your checks.
  • Treat HTTP_* values, REQUEST_URI and PHP_SELF as untrusted text: escape them before printing them into a page, and never use them to decide who someone is.
  • REMOTE_ADDR comes from the network connection, so it is harder to fake, but behind a proxy it is the proxy's address.

Some keys do not exist in every setup, for example when PHP runs from the command line, so read them with ?? too.

The shortcut

For every superglobal read, ask "who wrote this value?" The visitor, directly or through headers → validate and escape. The server's own configuration → generally safe.

Showing "you searched from ..." on a pageStep 1 of 4
  1. The value

    $_SERVER['REQUEST_URI'], such as /search.php?q=dune.

Drills

Answer fast — trust the rule.

quick drill

Which superglobal tells you whether the request was a GET or a POST?

quick drill

Why is reading form data from $_REQUEST discouraged?

quick drill

Who controls the value of $_SERVER['HTTP_USER_AGENT']?

quick drill

For /search.php?q=dune, what is $_SERVER['QUERY_STRING']?

Remember

  • Read input from the source you expect; treat request details as claims.
  • Shortcut: ask "who wrote this value?" before trusting it.
  • Trap: $_REQUEST blurs sources, and HTTP_* keys come straight from the visitor.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWho wrote this value?

A developer reads the order quantity from $_REQUEST['qty'] and prints $_SERVER['HTTP_USER_AGENT'] straight into an admin page. Explain the risk in each line and how to fix it.