Is $_SERVER['REQUEST_SCHEME'] reliable?

Is $_SERVER['REQUEST_SCHEME'] reliable?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Is $_SERVER['REQUEST_SCHEME'] Reliable?

When you need to determine the protocol of an incoming request, the instinct is often to check $_SERVER['REQUEST_SCHEME']. It appears straightforward: a simple array access that returns "http" or "https". However, relying on undocumented superglobals introduces significant risk. If this variable isn't listed in the official PHP documentation, it likely depends on specific SAPI (Server API) implementations rather than being a universal standard across all environments.

The reliability of $_SERVER['REQUEST_SCHEME'] hinges entirely on how your web server and PHP configuration interact. It is not a built-in PHP function that parses the raw request line; instead, it is populated by the web server module before passing control to PHP. On Apache with mod_php, this variable is generally available. However, its behavior becomes unpredictable when using FastCGI (FCGI) or CGI wrappers, which are common in containerized environments or specific hosting setups. In these scenarios, the variable might be missing entirely, defaulting to a fallback, or returning an incorrect value if the upstream proxy doesn't forward headers correctly.

Consider a typical deployment workflow where you are developing on Windows using IIS with PHP 5.4 and deploying to Ubuntu with Nginx or Apache. The source material highlights a developer using VC9 PHP 5.4.14 TS on Windows who found the variable present. This does not guarantee its presence on Ubuntu Linux. While the PHP core is consistent, the SAPI layer is where the divergence occurs. On Ubuntu, if you are running PHP via Nginx with php-fpm, the request passes through a FastCGI process. In many standard configurations for FPM, $_SERVER['REQUEST_SCHEME'] is not automatically set unless specific directives like fastcgi_param REQUEST_SCHEME are explicitly configured in your .conf file or virtual host settings.

Attempting to rely on this variable without validation can lead to subtle bugs. Imagine building a redirect logic where you assume the scheme is always present. If the variable is missing, accessing it might result in a notice warning, which could mask the actual application error. Worse, if your environment falls back to a default value, you might inadvertently serve traffic over HTTP when HTTPS is required, exposing sensitive data during transit.

The most robust approach involves checking multiple sources of truth rather than relying on a single undocumented flag. You should prioritize the $_SERVER['HTTPS'] variable, which is widely documented and standard across all SAPIs. If $_SERVER['HTTPS'] equals "on", the connection is secure. If it is empty or unset, you must inspect the incoming headers directly to determine if a proxy (like Nginx or Apache) is terminating SSL or just passing the traffic through.

A safe implementation pattern looks like this:

$protocol = 'http';

// Check for explicit HTTPS flag
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
    $protocol = 'https';
} elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
    // Handle cases where a reverse proxy sets the original scheme
    $protocol = 'https';
}

$url = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

This logic accounts for direct server connections and common proxy setups where X-Forwarded-Proto is used. It avoids the pitfalls of assuming $_SERVER['REQUEST_SCHEME'] exists or is correct in every environment, ensuring your application behaves consistently whether it runs on Windows, Ubuntu, or any other platform.

If you are building a large-scale application where session management and routing depend heavily on the correct scheme, platforms like Laravel provide helper methods that abstract these complexities. For instance, checking Illuminate\Http\Request::secure() handles the detection logic internally, ensuring that your code remains agnostic to the underlying server configuration. While this specific topic doesn't require a deep dive into framework internals, understanding the raw mechanics ensures you can debug issues when framework helpers aren't enough or when building custom middleware.

Our network