How to get http request origin in php

How to get http request origin in php

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

Stefan Bogdanescu

Founder & Senior Architect

How to get http request origin in php

When building an API that relies on shared secrets like App-ID and App-Secret for authentication, verifying the source of the request is often as critical as validating those credentials themselves. You need to ensure that the entity presenting your API key is actually the domain they claim to be. In a scenario where www.someone.com holds specific credentials, you must programmatically confirm that the incoming HTTP request originates from that exact host and not from an attacker spoofing the user-agent or simply guessing the secret.

In PHP, the server exposes this information through the global $_SERVER superglobal array. The primary attribute to inspect is $_SERVER['HTTP_HOST']. This variable contains the value of the Host header sent by the client. By comparing this value directly against your registered domains, you can enforce strict origin validation before accepting any authenticated payload.

Consider a straightforward implementation within an API controller or middleware. You would extract the host from the request and compare it against an allowed list for that specific App-ID. Here is how that logic typically looks in a procedural PHP context:

function validateRequestOrigin($app_id, $allowed_domains) {
    // Retrieve the Host header from the incoming request
    $request_host = $_SERVER['HTTP_HOST'];

    // Normalize both values to avoid case-sensitivity issues
    // Some browsers or proxies might send "WWW.EXAMPLE.COM" vs "www.example.com"
    $normalized_user_host = strtolower($request_host);
    
    foreach ($allowed_domains as $domain) {
        if (strtolower($domain) === $normalize_user_host) {
            return true; 
        }
    }

    // Host mismatch detected
    throw new \InvalidArgumentException('Origin host does not match registered domain for this API key');
}

It is crucial to handle the HTTP_HOST header with care. While it usually reflects the actual server name, clients can technically send arbitrary values in the Host header. If you rely solely on $_SERVER['HTTP_HOST'] without validating it against a whitelist, you are vulnerable to Host Header injection attacks. An attacker could potentially manipulate headers to trick your application into thinking they are coming from a trusted domain if your logic is too permissive.

Furthermore, remember that the Host header might be missing entirely in some rare edge cases or specific proxy configurations (though this is uncommon for standard HTTP). In such instances, checking $_SERVER['SERVER_NAME'] can serve as a fallback, although it represents the server's configured name rather than what the client explicitly requested. For strict API security, relying on the explicit Host header provided by the client is generally the correct approach when paired with a strict whitelist.

If you are using a framework like Laravel to handle your API logic, this same principle applies but is often abstracted through Request objects or Middleware. You can access the host via $request->server('HTTP_HOST'). This abstraction ensures that regardless of whether the value comes from $_SERVER, Apache mod_rewrite, or Nginx, you are inspecting the normalized header value provided by the client.

For high-security environments, simply checking the Host header might not be enough. You should consider implementing additional transport security measures, such as requiring the request to originate only from your specific subdomains (e.g., api.www.someone.com) and rejecting traffic from unrelated origins entirely. This multi-layered approach ensures that even if an API key is leaked, an attacker cannot easily reuse it from a different domain without also spoofing the Host header correctly, which becomes significantly harder when combined with strict allowlists.

Ultimately, validating the origin is a fundamental step in securing stateless authentication schemes. By cross-referencing the HTTP_HOST header against the registered domains associated with your API keys, you add a necessary layer of verification that protects your backend from unauthorized access attempts disguised as legitimate clients.

Our network