How I can get origin of request with PHP?

How I can get origin of request with PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

Retrieving Request Origin in PHP

When a client initiates an XMLHttpRequest (XHR) or fetch request from some-client.com to your API running on some-rest.com, determining the origin of that request is crucial for implementing Cross-Origin Resource Sharing (CORS) policies. The goal is to identify the domain name of the initiating application, not the IP address of the user's device.

In PHP, accessing this information relies entirely on HTTP headers sent by the browser or client library. The most direct and standard approach is to inspect $_SERVER['HTTP_ORIGIN']. This variable maps directly to the Origin header defined in the CORS specification (RFC 6454). If a user agent makes a cross-origin request, it automatically includes this header containing the scheme, host, and port of the origin.

// Example: Checking the Origin header
$origin = $_SERVER['HTTP_ORIGIN'] ?? null;

if ($origin) {
    echo "Request originated from: " . $origin; 
} else {
    // This might happen for same-origin requests where browsers omit the header
    echo "Same origin or no Origin header sent.";
}

However, relying solely on $_SERVER['HTTP_ORIGIN'] requires understanding browser behavior regarding same-origin requests. Modern browsers do not send the Origin header when a request is made to the exact same domain as the page currently loaded (same-origin policy). This is a security feature to prevent information leakage. Therefore, your server must be prepared to handle cases where this variable is absent. If it is missing, you can reasonably assume the request is either from the same origin or initiated programmatically without an origin context.

You might notice confusion between $_SERVER['HTTP_ORIGIN'], $_SERVER['HTTP_HOST'], and $_SERVER['REMOTE_ADDR']. It is vital to distinguish these:

  • $_SERVER['REMOTE_ADDR'] always contains the IP address of the client connecting to your server, often passed through a proxy or load balancer. It never contains domain names.
  • $_SERVER['HTTP_HOST'] reflects the host header sent by the client, which is usually the domain the user typed into their browser. For same-origin requests, this will match your current URL.
  • $_SERVER['SERVER_NAME'] is a server-side configuration variable derived from the virtual host setup. It does not reflect the actual requesting domain in cross-origin scenarios and should generally be ignored for origin detection.

A common pitfall occurs when using frameworks like Laravel. While native PHP handles these variables directly, framework middleware may normalize or mask them before your application logic runs. In a standard Laravel application, you might access this data via the request helper:

// Inside a Laravel controller or closure
$origin = request()->header('origin');

This is often cleaner than accessing the raw $_SERVER array directly. If you are using a specific package or library provided by LaravelCompany, they may offer utilities to simplify CORS header handling, but ensure you verify that these abstractions do not strip the origin data before processing.

Another critical consideration is security validation. Never blindly trust the Origin header for authentication decisions. While it identifies the source domain, a malicious actor could theoretically spoof this header in non-browser contexts (like curl or Postman). Always cross-reference the Origin with your allowed list of domains before granting access to sensitive resources. Additionally, if you are dealing with legacy clients that do not support CORS, they will not send an Origin header at all; relying on its absence to identify them can be risky without additional logic.

Finally, remember that the Origin header format is standardized as a scheme followed by a host (e.g., https://some-client.com:8080). If you only need the domain name without the protocol or port, you must parse this string using PHP's string manipulation functions or a dedicated parser to extract just the hostname part.

Our network