Server Variables
OxPHP builds $_SERVER from the incoming request following the CGI/1.1 specification. Process environment variables are imported first and the request's variables are set after them, so a request value always wins over an environment variable of the same name.
Standard variables
| Variable | Description | Example |
|---|---|---|
SCRIPT_FILENAME |
Absolute filesystem path to the PHP script being executed | /var/www/html/public/index.php |
DOCUMENT_ROOT |
The DOCUMENT_ROOT setting. With DOCUMENT_ROOT=false, the entry file's directory (getenv('DOCUMENT_ROOT') still returns false) |
/var/www/html/public |
SERVER_SOFTWARE |
Server identifier, with the running OxPHP version | OxPHP/0.16.0 |
SERVER_PROTOCOL |
Negotiated HTTP protocol version | HTTP/2 |
REQUEST_METHOD |
HTTP method | GET |
REQUEST_URI |
Full URI with query string | /app?page=2 |
SCRIPT_NAME |
Path of the executed script relative to DOCUMENT_ROOT. In Framework mode this is the front controller, not the request URI |
/index.php |
DOCUMENT_URI |
Alias for SCRIPT_NAME, for nginx/PHP-FPM compatibility |
/index.php |
PHP_SELF |
SCRIPT_NAME plus PATH_INFO when present, otherwise equal to SCRIPT_NAME |
/index.php/user/42 |
QUERY_STRING |
Query portion of the URI (empty string when absent) | page=2 |
SERVER_NAME |
Hostname of the request (see The request's host) | example.com |
SERVER_PORT |
Port of the request's host | 8080 |
REMOTE_ADDR |
Client IP address | 172.17.0.1 |
REMOTE_PORT |
Client port number | 54321 |
HTTPS |
"on" when the connection uses TLS; absent otherwise |
on |
REQUEST_SCHEME |
"https" for TLS connections, "http" otherwise |
https |
CONTENT_TYPE |
Value of the Content-Type header (no HTTP_ prefix) |
application/json |
CONTENT_LENGTH |
Value of the Content-Length header (no HTTP_ prefix) |
128 |
REQUEST_TIME |
Unix timestamp (integer) of when a PHP worker took the request. Time spent waiting for a free worker is not included | 1738800000 |
REQUEST_TIME_FLOAT |
The same moment as REQUEST_TIME, with microsecond precision |
1738800000.123456 |
GATEWAY_INTERFACE |
CGI version string | CGI/1.1 |
An elapsed time computed from REQUEST_TIME_FLOAT leaves out the wait for a worker, which under load can last up to QUEUE_WAIT_TIMEOUT_MS. The oxphp_queue_wait_us metric reports that wait.
The request's host
HTTP_HOST, SERVER_NAME, SERVER_PORT and the host of the request object come from the Host header, with these exceptions:
- HTTP/2. The client sends the host in the
:authoritypseudo-header, and OxPHP hands it to PHP as theHostheader. AHostheader sent next to it is replaced by it (RFC 9113 §8.3.1). - Absolute-form target. For
GET http://example.com/path HTTP/1.1(or HTTP/1.0), the host and port in the target replace theHostheader (RFC 9112 §3.2.2), andREQUEST_URIis the target's path and query. The request must still send a validHost. An empty path becomes/, soGET http://example.com?page=2gives aREQUEST_URIof/?page=2. The target's scheme is ignored:REQUEST_SCHEME,HTTPSand the default port are what they would be without it. - Behind a trusted proxy.
SERVER_NAME,SERVER_PORTand the request object's host come from the host the proxy forwards, over either protocol. - HTTP/1.0 without
Host.SERVER_NAMEislocalhostandSERVER_PORTis80(443for TLS).
A user@ or user:password@ in front of the host is not part of it. The Host header PHP gets leaves it out, and over HTTP/2 the comparison with a Host header ignores it.
Requests refused with 400
OxPHP answers these requests with 400 Bad Request without running PHP:
- An HTTP/1.1 request without a
Hostheader, or an HTTP/2 request with neitherHostnor:authority. - A
Hostheader sent on more than one line. Over HTTP/2,:authorityreplaces everyHostline that names the same host. - Over HTTP/2, a
Hostheader that names another host than:authority. Letter case does not count as a difference, but a port does:example.comandexample.com:443differ. - A
Host, absolute-form target or:authoritywhose host is empty (GET http://:80/path HTTP/1.1) or not valid (see below). - A request target in none of the forms HTTP defines:
- over HTTP/1.x, a target with neither a leading
/nor ascheme://, such asGET index.php HTTP/1.1(it reads as aCONNECTtarget, andCONNECTdoes not reach PHP either); - over HTTP/2, a
:paththat does not start with/, or no:pathin a request that has no:authorityeither; - a target of
*with any method butOPTIONS; - a target with no
//after its scheme, such asGET mailto:user@example.com HTTP/1.1, which has no host.
- over HTTP/1.x, a target with neither a leading
curl, wget, browsers and Kubernetes HTTP probes always send Host. A health check or script that writes HTTP/1.1 requests by hand to the main listener has to send it too. The internal listener (INTERNAL_ADDR) does not check Host.
A valid host is a name made of letters, digits, -._~, !$&'()*+,;= and % escapes such as %41, or an IPv6 address in brackets ([::1], or an IPvFuture address such as [v1.x]). It may be followed by : and a port from 0 to 65535 in digits (RFC 9110 §7.2). So these are refused:
Host: café.examplesent as raw UTF-8 (send the punycode form,xn--caf-dma.example, instead);Host: example.com:abcandGET http://example.com:abc/ HTTP/1.1;Host: :80;- an IPv6 zone, such as
Host: [fe80::1%25eth0].
An empty port, as in Host: example.com:, is valid and means the scheme's default: SERVER_PORT is 80 and HTTP_HOST is example.com:.
A few targets reach PHP in a special form:
OPTIONS *is routed as a request for the path*. Where that runs a script, itsREQUEST_URIis*.- A
:pathholding only a fragment, such as#top, arrives empty. Without an:authorityit is refused like a missing:path; with one, it is served as a request for/. - An HTTP/2 request with an
:authorityand no:pathnever reaches OxPHP: the HTTP/2 layer resets its stream.
HTTP request headers
HTTP request headers are added to $_SERVER with an HTTP_ prefix. Header names are converted to uppercase with dashes replaced by underscores, following CGI/1.1:
Accept: text/html -> HTTP_ACCEPT
X-Forwarded-For: 1.2.3.4 -> HTTP_X_FORWARDED_FOR
Authorization: Bearer abc -> HTTP_AUTHORIZATION
Cookie: session=xyz -> HTTP_COOKIEContent-Type and Content-Length appear without the prefix, as CONTENT_TYPE and CONTENT_LENGTH, because the CGI specification requires it.
A header whose name contains an underscore (_) or a dot (.) is not added to $_SERVER. PHP turns a dot in a key into an underscore, so X_Forwarded_For and X.Forwarded.For would both arrive as HTTP_X_FORWARDED_FOR. A client could then forge a key the application expects only from a proxy, such as HTTP_X_AUTHENTICATED_USER from an authentication proxy that sets X-Authenticated-User. nginx drops such headers by default too (underscores_in_headers off, ignore_invalid_headers on). To read one, ask the request object for the name as sent: $request->header('X_Api_Key').
A header sent as several lines with the same name, such as an X-Forwarded-For line from each of two proxies, becomes one key. Its lines are joined with , in the order they arrived (RFC 9110 §5.3), and Cookie lines with ; . The request object holds the same value. A header with a byte outside ASCII on any of its lines is left out of both $_SERVER and the request object.
Content-Type and Content-Length are the exception to both rules: CONTENT_TYPE and CONTENT_LENGTH, and the request object's values for them, are their first line, whatever the later lines hold. A Host sent on more than one line is refused.
Behind a reverse proxy
When TRUSTED_PROXIES is configured and the request peer is in the trusted set, OxPHP rewrites these keys from the forwarded headers (X-Forwarded-* or RFC 7239 Forwarded):
| Variable | Value when peer is trusted | Value otherwise |
|---|---|---|
REMOTE_ADDR |
Rightmost untrusted address in X-Forwarded-For / Forwarded |
Direct peer IP |
REMOTE_PORT |
Source port of the entry the address came from, or 0 |
Direct peer port |
HTTPS |
"on" when X-Forwarded-Proto: https |
Set only when the peer connection is TLS |
REQUEST_SCHEME |
"https" / "http" from X-Forwarded-Proto |
Based on actual TLS state |
SERVER_NAME |
Host part of X-Forwarded-Host |
Host part of the Host header |
SERVER_PORT |
X-Forwarded-Port, else the port of X-Forwarded-Host, else 443/80 by scheme |
Port of Host, or 443/80 |
REMOTE_PORTis the client's source port when the forwarded entry carries one (aForwarded: for="ip:port"node, or anip:portentry inX-Forwarded-For). Otherwise it is"0": zeroed rather than guessed.- With
cloudflareinTRUSTED_PROXIES, a request whose forwarding chain shows it arrived from a Cloudflare edge takesREMOTE_ADDRfromCF-Connecting-IP. That header carries no port, soREMOTE_PORTis"0"even whenForwardedhas one. - When the forwarding headers yield no client address (see Client IP extraction), neither
REMOTE_ADDRnorREMOTE_PORTis rewritten. - The raw
HTTP_X_FORWARDED_FOR,HTTP_X_FORWARDED_PROTO,HTTP_X_FORWARDED_HOST,HTTP_X_FORWARDED_PORTandHTTP_FORWARDEDkeys stay in$_SERVERunchanged.
Without TRUSTED_PROXIES nothing is rewritten, and REMOTE_ADDR is the direct peer, usually your load balancer rather than the client. Parsing X-Forwarded-For yourself is error-prone (leftmost vs rightmost, no CIDR trust check), so configure TRUSTED_PROXIES instead. See Trusted Proxies for the trust algorithm and syntax.
Trace context variables
When distributed tracing is enabled, OxPHP adds trace context variables to $_SERVER:
| Variable | Description | Example |
|---|---|---|
OXPHP_TRACE_ID |
W3C trace ID for the current request | 4bf92f3577b34da6a3ce929d0e0e4736 |
OXPHP_SPAN_ID |
Span ID for the OxPHP server span | 00f067aa0ba902b7 |
OXPHP_PARENT_SPAN_ID |
Parent span ID from the upstream service (empty if root) | b9c7c989f97918e1 |
They are present only when a valid traceparent header arrives or OxPHP starts a new trace. Without tracing configured, these keys are absent.
Differences from PHP-FPM
These variables behave differently from a standard nginx + PHP-FPM setup:
| Variable | Behavior |
|---|---|
SERVER_ADDR |
Not set. OxPHP does not populate the local server IP address. |
PATH_INFO |
Set automatically; see PATH_INFO behavior. |
PATH_TRANSLATED |
Not set. nginx and PHP-FPM do not set it by default either. |
PHP_AUTH_USER / PHP_AUTH_PW / AUTH_TYPE |
Not extracted from the Authorization header. Read $_SERVER['HTTP_AUTHORIZATION'] directly. |
REDIRECT_STATUS |
Not set. OxPHP has no internal redirect mechanism. |
Request handling also differs from nginx in a few edge cases:
- For an absolute-form target, nginx passes PHP-FPM the
Hostheader it received asHTTP_HOST, and?page=2rather than/?page=2asREQUEST_URIwhen the path is empty. - nginx accepts a
Hostwith a%that starts no escape (a%zz) or with brackets around anything but an IPv6 or IPvFuture address ([],[1.2.3.4],[a.example]). OxPHP refuses them. - nginx refuses a
Hostwith an empty label (a..b), which OxPHP accepts. - nginx refuses
OPTIONS *, a fragment-only:pathwith an:authority, and an HTTP/2 request with an:authorityand no:path, with400.
Example
<?php
$method = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];
$ip = $_SERVER['REMOTE_ADDR'];
$host = $_SERVER['SERVER_NAME'];
$scheme = $_SERVER['REQUEST_SCHEME']; // "http" or "https"
// Read a custom header
$token = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
// REMOTE_ADDR is already the real client IP when TRUSTED_PROXIES is configured.
// Without it, REMOTE_ADDR is the direct peer (usually a load balancer).
$clientIp = $_SERVER['REMOTE_ADDR'];
// Check TLS without checking the port
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
// Secure connection
}PATH_INFO behavior
$_SERVER['PATH_INFO'] is set according to the routing mode. There is no setting for it (SPLIT_PATH_INFO_ENABLED no longer exists).
| Routing mode | When set | Value |
|---|---|---|
Traditional (ENTRY_FILE unset) |
Only when the URI contains .php/ and the script prefix exists on disk |
Tail after the script segment |
Framework (ENTRY_FILE=index.php) |
Only when the request names the entry file with a trailing segment (/index.php/extra) |
Tail after the entry file, e.g. /news |
SPA (ENTRY_FILE=index.html) |
Never. PHP only runs for exact .php files |
— |
SCRIPT_NAME always names the executed script, so PATH_INFO is present only when SCRIPT_NAME is a literal prefix of the request path. When a request is rewritten to a front controller it does not name (an application route, a directory index, a static-miss fallback), PATH_INFO is absent and the original path is in REQUEST_URI. The PHP_DENY_PATHS fallback is the exception: it sets PATH_INFO to the original sanitized URI, so the fallback script can route on it.
Traditional mode examples
OxPHP scans the URI left to right for the first .php segment that is a file on disk. Everything after it becomes PATH_INFO:
| Request URI | File on disk | SCRIPT_NAME |
PATH_INFO |
PHP_SELF |
|---|---|---|---|---|
/app.php/user/42 |
app.php exists |
/app.php |
/user/42 |
/app.php/user/42 |
/index.php/api/v2/users |
index.php exists |
/index.php |
/api/v2/users |
/index.php/api/v2/users |
/app.php |
app.php exists |
/app.php |
(absent) | /app.php |
/missing.php/foo |
file not found | falls back to /index.php |
— | depends on fallback |
Framework mode examples
Every non-static request is rewritten onto index.php. PATH_INFO is set only when the request names the entry file with a trailing segment; for application routes, read the original path from REQUEST_URI.
| Request URI | SCRIPT_NAME |
PATH_INFO |
|---|---|---|
/api/users |
/index.php |
(absent) |
/about.php |
/index.php |
(absent) |
/index.php/news/local |
/index.php |
/news/local |
/index.php |
/index.php |
(absent) |
Prompts for your agent
These prompts assume an agent with the OxPHP documentation MCP server connected. The agent reads the pages it needs itself.
Audit my code's `$_SERVER` reads for what differs between OxPHP and PHP-FPM. Read the `php/server-variables` page through the OxPHP documentation MCP server.
Check my application for `$_SERVER['PHP_AUTH_USER']`, `SERVER_ADDR`, `PATH_TRANSLATED` or `REDIRECT_STATUS` reads, Basic-Auth code that expects the credentials pre-parsed, and code that parses `X-Forwarded-For` itself instead of relying on `TRUSTED_PROXIES`, then tell me which reads need to change and to what.See also
- Superglobals:
$_GET,$_POST,$_COOKIE,$_ENVand the other superglobals, and how to disable them - HTTP Request API: typed access to the same request data
- Trusted Proxies: which
$_SERVERkeys a trusted proxy's headers override