Cross-Origin Request Headers(CORS) with PHP headers

Cross-Origin Request Headers(CORS) with PHP headers

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

Stefan Bogdanescu

Founder & Senior Architect

The "Catch-All" Header Fallacy in CORS Implementations

Developers frequently encounter a specific CORS configuration error when trying to serve resources from a PHP backend to a different domain. The symptom is immediate: the browser blocks the request with an error stating that a required header, typically X-Requested-With, is not allowed by Access-Control-Allow-Headers.

The source of confusion often lies in the instinctive solution to make headers permissive: using the wildcard asterisk (*). In your PHP script, you likely wrote something resembling this:

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
// ...

While intuitive, setting Access-Control-Allow-Headers to * is fundamentally incorrect according to the CORS specification. The browser will not accept a wildcard for headers; it strictly enforces an explicit list. When you send *, the browser ignores that specific header line and proceeds with its own default allowlist, which usually contains only simple headers like Accept, Accept-Language, and Content-Type. Consequently, any custom header your JavaScript sends—such as X-Requested-With or an Authorization token—is rejected immediately.

To resolve this, you must explicitly list every non-simple header that your frontend application intends to send. If your API requires the Authorization header for authentication and your AJAX calls include X-Requested-With, your PHP response header must look like this:

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Authorization, X-Requested-With, Content-Type");

This approach is verbose but secure. It ensures that the browser validates the incoming request against your specific whitelist before allowing the response to reach the client. Attempting to use * for headers effectively bypasses this security check in the browser's logic, leading to the exact error you are seeing.

Handling Preflight Requests

The error message typically appears during a "preflight" request. Before a browser sends a cross-origin POST or PUT request with custom headers, it first fires an OPTIONS request to your server to verify that the actual request is allowed. Your PHP script must handle this OPTIONS method correctly and return the necessary headers before processing the main logic.

If you are using a framework like Laravel, you can utilize its built-in middleware to handle this automatically without writing repetitive header code:

// In your routes file or within your controller
use Illuminate\Support\Facades\Cors;

Cors::any()->withOrigins(['your-frontend-domain.com']);

For raw PHP scripts, ensure your logic is placed at the very top of the script before any output is sent. The headers must be defined on the first call to header(). If you accidentally echo content or include a file that outputs data before setting these headers, PHP will throw a "headers already sent" error, and the CORS configuration will fail silently or partially.

Security Considerations for Wildcards

While setting Access-Control-Allow-Origin: * is convenient for public APIs, it introduces a specific risk: any website on the internet can make requests to your API. This is often acceptable if your API is stateless and does not handle sensitive user data or require authentication. However, if you are handling private data, you should replace * with the specific origin of your frontend application (e.g., https://your-frontend-domain.com).

Furthermore, remember that Access-Control-Allow-Credentials: true cannot be used in conjunction with a wildcard origin. If your JavaScript fetch request includes credentials like cookies or HTTP authentication headers, you must specify the exact domain allowed. Mixing these settings results in another blocked request, as the browser prioritizes security over convenience when credentials are involved.

By switching from wildcards to explicit header lists and correctly handling preflight requests, you align your PHP backend with the strict requirements of the CORS standard, eliminating the X-Requested-With error and ensuring cross-origin communication works as intended.

Our network