PHP: HTTP or HTTPS?
Determining whether a request arrived over HTTP or HTTPS is a fundamental requirement for implementing secure redirects, enforcing mixed-content policies, and managing session security flags like SESSION_SECURE. In most modern web frameworks, this information is exposed through the protocol scheme available to your application logic.
In standard PHP scripts running outside of a framework, you access this data via the $_SERVER superglobal. Specifically, the $_SERVER['HTTPS'] variable holds the string "on" or "true" if the server detects an SSL/TLS connection. However, relying solely on this variable can be brittle due to how web servers configure environment variables. If your load balancer terminates SSL and forwards requests to your backend application over HTTP, $_SERVER['HTTPS'] might return an empty string even though the end-user originally connected via HTTPS.
To handle this robustness, you must check the original request scheme using $_SERVER['HTTP_X_FORWARDED_PROTO']. Many reverse proxies like Nginx or Apache set this header to "https" when they terminate the SSL layer upstream. Your code should inspect both sources: first checking if $_SERVER['HTTPS'] is explicitly set to a truthy value, and falling back to checking the forwarded protocol header if that fails.
For modern applications using the Laravel framework, the concern shifts slightly from raw PHP variables to the HTTP message abstraction layer. The Illuminate\Http\Request class provides a dedicated method for this exact scenario: $request->secure(). This method intelligently inspects the incoming request headers and the underlying server environment to determine if the current connection is secure.
If you are building a custom redirect logic, mixing these approaches incorrectly can lead to security vulnerabilities. Consider a common pitfall where a developer attempts to force HTTPS by checking only $_SERVER['HTTPS'] in a script running behind an Nginx reverse proxy. If the Nginx configuration does not explicitly set $http_x_forwarded_proto, the PHP script will see no indication of SSL, potentially redirecting users who are already on HTTPS back to HTTP or failing to redirect those on HTTP at all.
Here is a pattern for handling this in raw PHP that accounts for reverse proxies:
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
$protocol = $_SERVER['HTTP_X_FORWARDED_PROTO'];
}
$url = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
In Laravel, the syntax is cleaner but the underlying logic remains important for debugging:
if ($request->secure()) {
// Handle HTTPS logic
} else {
return redirect()->secure($currentUrl);
}
There are specific trade-offs to consider when enforcing this behavior. Forcing users from HTTP to HTTPS via a redirect is excellent practice, but it can cause issues if the redirection loop occurs because the browser caches the initial non-secure request or if the server certificate validation fails silently in development environments. Additionally, checking the X-Forwarded-Proto header requires trust; if you are running behind multiple layers of proxies, ensuring that only trusted headers are forwarded is critical to prevent header injection attacks.
When dealing with legacy systems or specific hosting environments, you might encounter cases where $_SERVER['HTTPS'] returns an empty string despite a valid SSL connection. This usually indicates that the server software (like Apache or Nginx) did not set the environment variable correctly, or it was stripped by a proxy. In such scenarios, relying exclusively on $_SERVER['HTTPS'] will result in incorrect protocol detection.
For developers looking to manage complex redirect rules and ensure their applications handle mixed content gracefully, understanding these nuances is vital. If you need deeper guidance on configuring your web server to pass the correct headers or managing SSL termination strategies within a larger infrastructure, resources like https://laravelcompany.com can provide further insights into best practices for modern PHP application deployment.