Detecting SSL With PHP

Detecting SSL With PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Detecting SSL With PHP

Determining whether a request arrives over HTTPS in PHP isn't as straightforward as checking a single variable, especially when your application runs on diverse infrastructure. While $_SERVER['HTTPS'] is the most common starting point, relying on it exclusively can lead to false positives and negatives depending on the web server configuration. To build robust redirects or secure logic that works everywhere, you need to understand how different servers signal SSL termination and how to synthesize that data reliably.

The $_SERVER['HTTPS'] Flag

The standard approach involves checking the $_SERVER['HTTPS'] variable. When a browser sends an HTTPS request, most web servers (like Apache or Nginx) set this variable to "on" if the connection is secure. However, this behavior is not guaranteed by default in all environments.

For instance, some legacy configurations or specific reverse proxy setups might leave this variable unset even when using SSL. If you encounter a case where $_SERVER['HTTPS'] is empty but you are clearly behind a load balancer handling TLS, checking solely for this flag will cause your logic to fail. This is why many developers look for alternative indicators before writing their conditional checks.

The REQUEST_SCHEME Environment Variable

A more reliable indicator often lives in the $_SERVER['REQUEST_SCHEME'] variable. Many modern web servers, particularly Nginx and certain Apache configurations using the X-Forwarded-Proto header, populate this variable based on the actual protocol used by the client. Unlike HTTPS, which is a specific flag that can be toggled off in configuration files, REQUEST_SCHEME explicitly states whether the scheme is http or https.

However, you must ensure your server is actually configured to send this header. If you are behind a CDN like Cloudflare or a corporate reverse proxy, they typically forward the protocol information via headers such as X-Forwarded-Proto or X-Forwarded-Ssl. Your web server then needs to be configured to read these headers and populate REQUEST_SCHEME or HTTPS accordingly. Without this configuration, your PHP script will see plain HTTP logic despite the traffic being encrypted.

Synthesizing a Robust Detection Logic

Because no single variable is universally consistent across all hosting providers, the most robust solution combines multiple checks. You should prioritize explicit scheme detection but fall back to the HTTPS flag if necessary. Here is a pattern that handles the majority of edge cases:

function isSecure(): bool {
    // Priority 1: Explicit scheme variable (often set by Nginx/Apache with proper headers)
    $scheme = $_SERVER['REQUEST_SCHEME'] ?? null;
    
    if ($scheme === 'https') {
        return true;
    }

    // Priority 2: The standard HTTPS flag
    if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
        return true;
    }

    // Priority 3: Check for specific proxy headers if the above are missing
    // This covers scenarios where REQUEST_SCHEME is unset but headers exist.
    $forwardedProto = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? null;
    $xForwardedSsl = $_SERVER['HTTP_X_FORWARDED_SSL'] ?? null;

    return ($forwardedProto === 'https' || $xForwardedSsl === 'on');
}

This approach avoids the trap of assuming !empty($_SERVER['HTTPS']) is sufficient. By checking REQUEST_SCHEME first, you capture modern server configurations that explicitly declare the protocol. The fallback to HTTP_X_FORWARDED_PROTO ensures compatibility with CDNs and load balancers that might not set REQUEST_SCHEME but do provide the standard proxy headers.

Configuration Matters

The code above will only work if your server environment correctly interprets incoming TLS connections. If you are self-hosting, ensure your Nginx or Apache configuration includes directives like proxy_set_header X-Forwarded-Proto $scheme; (for Nginx) or the equivalent for Apache to pass the original protocol upstream. Without this, every request appears as HTTP to your backend application regardless of whether it arrived over TLS.

When redistributing scripts across different environments, testing with a dedicated HTTPS endpoint is crucial. Relying on a single detection method can introduce subtle bugs that only surface on specific hosting platforms. By implementing a layered check, you mitigate the risk of broken redirects or insecure logic leaks in production.

Our network