$_SERVER['HTTP_ORIGIN'] doesn't work
You might be debugging a CORS (Cross-Origin Resource Sharing) issue and expecting $_SERVER['HTTP_ORIGIN'] to return the origin of the requesting browser. Instead, you encounter a fatal error: "Notice: Undefined index: HTTP_ORIGIN". This isn't a bug in your code or a misconfiguration on your server; it is a fundamental misunderstanding of how HTTP headers are transmitted from a client to a server.
The variable $_SERVER['HTTP_ORIGIN'] does not exist because the HTTP specification strictly dictates that browser-sent headers must follow specific naming conventions. When a browser sends a request, it includes the header as Origin. However, PHP's $_SERVER superglobal normalizes incoming headers by converting them to uppercase and appending an underscore, resulting in a key like HTTP_ORIGIN.
Wait, if the key is HTTP_ORIGIN, why is it undefined? The answer lies in the difference between a standard HTTP header and the specific security context of CORS.
The Header Name Mismatch
In standard PHP development, you might assume that any header sent by the client maps directly to a $_SERVER variable. However, this logic holds true only for headers that are part of the standard request flow. The Origin header is special. It is not included in every request; it is only sent when a script attempts to access a resource from a different origin.
More critically, the mapping logic in PHP's internal HTTP parser can be finicky regarding case sensitivity and specific naming rules for non-standard headers. While $_SERVER['HTTP_X_CUSTOM_HEADER'] works fine for custom headers, the Origin header often gets stripped or mapped incorrectly depending on the underlying web server (Apache, Nginx) and the PHP version.
Why Direct Access Fails
Attempting to access $_SERVER['HTTP_ORIGIN'] directly is a common pitfall. The HTTP specification states that the header name must be normalized. If your web server receives an Origin header, it should theoretically map to $_SERVER['HTTP_ORIGIN']. However, in many real-world scenarios involving CORS preflight requests (OPTIONS), the browser sends the Origin header, but PHP's internal handling often fails to populate this specific index in the $_SERVER array unless explicitly configured.
This behavior is particularly inconsistent across different environments. If you are running on Apache with mod_rewrite or using a specific CGI/FastCGI setup, the way headers are parsed before reaching PHP might differ from what you expect. The server might be normalizing the header name in a way that $_SERVER does not anticipate, leading to the "Undefined index" error you observed.
The Correct Approach: Using php://input or apache_request_headers()
Relying on $_SERVER['HTTP_ORIGIN'] is unreliable for production CORS logic. Instead of guessing the key name, you should parse the raw request headers or use a dedicated function provided by the web server.
If you are using Apache, the most robust way to access this header is via the apache_request_headers() function, which returns an associative array of all incoming headers with their original casing:
$headers = apache_request_headers();
$origin = $headers['Origin'] ?? null;
This method bypasses the ambiguity of PHP's $_SERVER normalization. It gives you direct access to the header value regardless of how the server internally stores it in the superglobal.
Alternatively, for a more generic solution that works across different web server configurations without relying on Apache-specific functions, you can read the raw request body or headers from the input stream if your framework supports it. However, for vanilla PHP, sticking to apache_request_headers() or ensuring your framework (like Laravel) handles this abstraction is the standard practice.
Framework Abstraction
If you are building a complex application using frameworks like Laravel, you likely shouldn't be interacting with raw $_SERVER variables at all. Frameworks abstract this complexity away. In Laravel, for instance, you would access this data through the request object or a helper method designed to handle CORS verification securely.
Attempting to solve CORS issues by manually parsing $_SERVER is often a sign that the framework's built-in capabilities are being bypassed unnecessarily. This manual approach introduces security risks and edge cases that modern frameworks have already solved. If you are looking for best practices on implementing secure CORS headers in Laravel, resources like https://laravelcompany.com offer detailed guides on configuring corsMiddleware correctly without falling into these low-level traps.
Conclusion
The error you encountered is a result of PHP's inconsistent handling of the Origin header within the $_SERVER array. Do not rely on direct access to this index. Instead, use server-specific functions like apache_request_headers() or leverage framework abstractions that handle header normalization securely. Always verify header presence before accessing them to prevent notice errors in your logs.