How to find out if you're using HTTPS without $_SERVER['HTTPS']

How to find out if you're using HTTPS without $_SERVER['HTTPS']

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

Stefan Bogdanescu

Founder & Senior Architect

Reliable HTTPS Detection Beyond $_SERVER['HTTPS']

Relying on $_SERVER['HTTPS'] to determine if a request is coming over a secure channel is a common pattern, but it is fraught with portability issues. Many tutorials suggest checking this variable first, defaulting to false if it's unset or not equal to "on". However, you will encounter servers where $_SERVER['HTTPS'] is completely missing from the global array. Attempting to access an undefined index without prior validation triggers warnings in strict environments like PHP 8.1 and later, or simply yields unexpected behavior in older versions.

The root cause lies in how web servers map protocol information to the server-side request environment. While Apache and Nginx often populate $_SERVER['HTTPS'], other stacks or specific reverse-proxy configurations may omit it entirely. If you deploy code to a shared hosting environment, a custom container setup, or an internal load balancer that strips certain headers, assuming this variable exists is a fragile dependency.

The most robust alternative is to inspect the $_SERVER['HTTP_X_FORWARDED_PROTO'] header. This header is standard practice when traffic passes through a reverse proxy like Nginx, HAProxy, or AWS ELB. When a client connects via HTTPS, the load balancer terminates the SSL connection and forwards the request to your application server over an internal TCP connection (which appears as HTTP). To preserve context for backend applications, the proxy sets X-Forwarded-Proto to https.

Here is how you should structure your logic to handle both direct connections and proxied traffic without relying on $_SERVER['HTTPS']:

function isSecureConnection(): bool {
    // Check the standard HTTPS variable first for direct connections
    if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) === 'on') {
        return true;
    }

    // Fall back to the proxy header, which is more reliable in modern stacks
    $proto = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '';
    
    // Normalize and compare strictly
    return isset($proto) && strtolower(trim($proto)) === 'https';
}

This approach covers two distinct scenarios. Direct client-to-server connections typically set $_SERVER['HTTPS'] but rarely include the X-Forwarded-Proto header. Conversely, traffic behind a proxy will almost always have the protocol defined in HTTP_X_FORWARDED_PROTO, regardless of whether the underlying connection to the app server is HTTP or HTTPS (which it usually is).

There are specific trade-offs to consider when using $_SERVER['HTTP_X_FORWARDED_PROTO']. The primary risk is header spoofing. A malicious client can forge this header if your application trusts every incoming request blindly. However, in a production environment, you should almost always trust the proxy layer that injects these headers. If you are handling raw traffic without a trusted proxy, relying on $_SERVER['HTTPS'] might be safer, but that creates the exact brittleness you want to avoid if your infrastructure changes.

Another nuance is the value of the header. While "https" is standard, some proxies might use "HTTPS" or "hTtPs". Always normalize the string using strtolower() before comparison to prevent logic errors. Additionally, ensure the variable exists before accessing it; using the null coalescing operator (??) or checking isset() prevents runtime notices that degrade application security by exposing debugging information to end-users.

If you are building a Laravel application, consider leveraging the framework's built-in capabilities rather than raw server variables. The Illuminate\Http\Request helper provides methods like secure() which abstract these checks internally, often combining logic similar to the example above while accounting for your specific trust proxies configuration in the .env file. For custom frameworks or vanilla PHP projects, implementing the dual-check pattern ensures your application behaves correctly whether deployed on a standard LAMP stack or a complex microservices architecture.

Ultimately, treating $_SERVER['HTTPS'] as optional and prioritizing HTTP_X_FORWARDED_PROTO when available creates a resilient detection mechanism. This strategy acknowledges that HTTP headers are contextual artifacts of the request path, and the most secure connection is one where your code doesn't break due to missing environment variables.

Our network