Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Variables and Constants

PHP $_SERVER superglobal

Reference for PHP $_SERVER: REQUEST_METHOD, REQUEST_URI, HTTP_* headers, SCRIPT_NAME, argv and more, which keys the web server sets and which you can trust.

$_SERVER is an array with information about the request, the web server and the running script. The entries are created by the web server and PHP, so the exact set depends on the setup (Apache, nginx with PHP-FPM, the built-in server, or the command line). No list of keys is guaranteed everywhere, so always read with a default.

Some entries describe your own environment and are safe (SCRIPT_FILENAME, DOCUMENT_ROOT); others are copied from what the client sent (HTTP_USER_AGENT, HTTP_HOST, REQUEST_URI, HTTP_REFERER) and are as untrustworthy as $_GET.

PHP
<?php
$method = $_SERVER["REQUEST_METHOD"] ?? "CLI";
$path = parse_url($_SERVER["REQUEST_URI"] ?? "/", PHP_URL_PATH);

Try it

On the command line there is no web server, so only the script-related keys exist. The second example fills in typical web values to show how they are used.

PHP What exists when running from the command line

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 Routing and headers with simulated values

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
$_SERVER
$_SERVER["KEY"]

Common keys:

Key Meaning
REQUEST_METHOD The HTTP method: GET, POST, PUT, DELETE...
REQUEST_URI Path and query string as requested, such as /blog?page=2.
QUERY_STRING The part after ?, without the ?.
SCRIPT_NAME, PHP_SELF Path of the running script relative to the web root.
SCRIPT_FILENAME Absolute file system path of the running script.
DOCUMENT_ROOT The web root directory configured on the server.
HTTP_HOST The Host header the client sent.
SERVER_NAME The name from the server configuration.
HTTPS Set to a non-empty value (not off) on HTTPS connections with most servers.
REMOTE_ADDR IP address of the connecting client or proxy.
HTTP_USER_AGENT, HTTP_REFERER, HTTP_ACCEPT_LANGUAGE Request headers sent by the client.
CONTENT_TYPE, CONTENT_LENGTH Request body type and size (no HTTP_ prefix).
REQUEST_TIME, REQUEST_TIME_FLOAT Start of the request as a Unix timestamp (int and float).
argv, argc Command-line arguments and their count (command line only).

Type: array<string, mixed>

Usage notes

  • Read with a default. Keys vary by server and SAPI. Use $_SERVER["X"] ?? null and never assume REQUEST_METHOD exists, because it does not in the command line.
  • Request headers become HTTP_*. User-Agent is HTTP_USER_AGENT. Content-Type and Content-Length are the exceptions, they appear as CONTENT_TYPE and CONTENT_LENGTH.
  • Do not trust client-controlled keys. HTTP_HOST, HTTP_REFERER, HTTP_USER_AGENT, HTTP_X_FORWARDED_FOR and REQUEST_URI can all be forged. Escape them before printing, and never use HTTP_HOST to build password-reset links without checking it against an allow list.
  • REMOTE_ADDR behind a proxy. It shows the proxy. Only read X-Forwarded-For or similar headers if the proxy is yours and you know it overwrites them.
  • PHP_SELF is dangerous in HTML. It includes extra path text the client adds (/form.php/"><script>), so <form action="<?= $_SERVER['PHP_SELF'] ?>"> is an XSS hole. Use an empty action or escape the value.
  • Method override. Browsers only send GET and POST from forms. Frameworks fake other methods with a hidden field; plain $_SERVER["REQUEST_METHOD"] shows the real one.
  • Use REQUEST_TIME_FLOAT for timing. microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"] is the elapsed time since the request started.
  • Command line. $_SERVER["argv"] is the same as the global $argv; argv[0] is the script name.
  • Assignments stay in your script. Changing $_SERVER does not change the web server or the real request.
  • getallheaders() exists on Apache and PHP-FPM, and returns request headers with their original names. $_SERVER works everywhere.